2

心に留めておいていただきたいのは、私は検索するキーワードを知るための語彙をまだ十分に持っていない (今はコーディングの時流に飛び乗っているだけです...) ので、これは答えるのが本当に簡単な質問かもしれません!

私はオブジェクトの配列を持っています

M = [A_1, A_2, ..., A_n]

、ここで A_i はクラスのインスタンス化です

A = {text: "string", coords:[x,y]}

私の質問は次のとおりです。配列 M にオブジェクト A_i が含まれているかどうかを確認する、すばやくエレガントで簡単な方法は何ですか?

A_i.text="your mom" && A_i.coords = [9,9]

本当ですか?

いくつかの気の利いた方法があるように見えるunderscore.jsにアクセスできますが、それを実装する方法について少し混乱しています...

私が試したこと(underscore.jsライブラリを使用)は次のとおりです。

var isitso = _.find(M, function (i) { 
    return i.text == "your mom" && i.coords == [9,9];
});

JS を学習するこの段階で、この問題を解決するためのさまざまな方法を評価したいと思います。何がどのようにできるかを確認するだけです。

前もって感謝します!

4

6 に答える 6

3

あなたが持っていることの難しさは、配列の等価性が参照の等価性 (同じメモリ位置でなければならない) であり、値の等価性 (同じ値を持つ) ではないことです。チェックを次のように変更した場合:

      return i.text == 'your mom' && i.coords[0] == 9 && i.coords[1] == 9;

それはうまくいくはずです。この場合、配列オブジェクト自体を比較せずに、最初の (0) 要素と 2 番目の (1) 要素に必要な値があるかどうかを確認します。数値を比較すると、参照ではなく値が比較されます。

于 2012-11-25T06:10:36.280 に答える
1

配列が等しいかどうかをチェックすることはできません。次のようなことをする必要があります:

_.find(M, function (i) { 
    return i.text == "your mom" && i.coords[0] == 9 && i.coords[1] == 9;
});
于 2012-11-25T06:11:00.397 に答える
0

_.filterメソッドを使用します。

var filtered = _.filter(M, function(i) { return (i.text == "text2" && i.cords[0] == 3 && i.cords[1] == 4) });
于 2012-11-25T06:09:05.627 に答える
0

アンダースコア js について聞いたことがないので、その詳細について話すことはできません。ほとんどの javacript フレームワークには、まだ標準化されていない foreach ループをエミュレートするために、何らかのタイプの foreach メソッドがあります。

function findMatches(/* target array*/ target, /* matching function*/ match) {
   var dummy = [];
   dojo.forEach(target, function(t) {
        if (match(t)) {
            dummy.push(t);
        }
   });
   return dummy;
}

var allMatches = findMatches(M, function (m) {
    m.text == "your mom" && m.coords == [9,9]
});

dojo.forEach私が使用する Dojo ライブラリーの forEach であることに注意してください。jQuery にはお好みのライブラリのようなものも含まれていません。

遅くなりましたが、このコードを実際のブラウザーに入れたことはありませんが、タイプミスをしたとしても、アイデアは適切です。

于 2012-11-25T06:11:07.263 に答える
0

All of the other answers so far are using third-party js libraries. The following is an attempt at a straight js solution. Fiddle: http://jsfiddle.net/rNfph/

var M = [
    {text:'one', coord: [0,0]},
    {text:'two', coord: [1,1]},
    {text:'three', coord: [2,2]},
    {text:'four', coord: [3,3]},
    {text:'five', coord: [4,4]},
    {text:'six', coord: [5,5]},
    {text:'seven', coord: [6,6]},
    {text:'eight', coord: [7,7]},
    {text:'nine', coord: [8,8]},
    {text:'ten', coord: [9,9]}
];

var search = function(str, x, y){

    for(var i=0, len=M.length; i<len; i++){
        if( M[i].text === str && M[i].coord[0] === x && M[i].coord[1] === y ){
            return true;
        }
    }
    return false;

}

console.log( search('three', 2, 2) );
console.log( search('three', 1, 2) );

Now, take this with a grain of salt. I am not an expert in complexity. There very well could be better ways to do this since this implementation requires iteration through potentially all of the values.

If there was a restriction on this where the value of 'text' is required to be unique, then instead of using an array, you could structure it all as an object (dictionary) and do the checks without needing any iterations at all.

See this fiddle: http://jsfiddle.net/4mrsh/

var M = {
    'one': [0,0],
    'two': [1,1],
    'three': [2,2],
    'four': [3,3]
}

var search = function(key, x, y){

    if( !M[key] || !(M[key][0] === x && M[key][1] === y) ) return false;

    return true;
}

console.log( search('three',2,2) ); //true
console.log( search('three',1,2) ); //false

​</p>

于 2012-11-25T06:49:19.430 に答える
0
if (!Array.prototype.each) {
   Array.prototype.each = function(callback) {
      var i, l = this.length;
      for (i = 0; i < l; i += 1) {
         if (callback(i, this[i]) === true) return;
      }
   }
}

function anMHasYourMom(m) {
   var found;
   m.each(function(i, el) {
      return found = (
         el.text === "your mom"
         && el.coords[0] === 9
         && el.coords[1] === 9
      );
   });
   return found;
}

var M = [A_1, A_2, ..., A_n];
if (anMHasYourMom(M)) {
   window.alert('It has one!');
}
于 2012-11-25T19:12:23.530 に答える