2017-02-11 10 views
-1

私はオブジェクトの配列を持っていて、各オブジェクトを検索し、そのデータが与えられた値と一致するかどうかをチェックしてオブジェクトを配列から戻したいのですが、見つからなければundefinedを返します。私は条件がなく、else文の一致した場合に、それは配列オブジェクトを返すに私にエラーを与えた場合、オブジェクトを返すようにしようとしているオブジェクトを配列から戻すにはどうすればよいですか?

var data = [{ 
    id: 1, 
    firstName: 'John', 
    lastName: 'Smith' 
}, { 
    id: 2, 
    firstName: 'Jane', 
    lastName: 'Smith' 
}, { 
    id: 3, 
    firstName: 'John', 
    lastName: 'Doe' 
}]; 
var asf[] = data[0]; 
return asf; 

は、これは私がこれまで持っているものです。

_.findwhere(data, pid)私はアンダースコアのモジュールのメソッドですが、配列からオブジェクトを返すために使用することもできますか?

+0

「retur n個のオブジェクトが配列から外れています "。それを削除しますか? – Carcigenicate

+0

var findObjectWithThisValue = 3; VARデータ= [{ 番号:1、 のfirstName: 'ジョン'、 lastNameの 'スミス' }、{ ID:2、 のfirstName 'ジェーン' lastNameの 'スミス' }、{ id:3、 firstName: 'John'、 姓: 'Doe' ]]; function SearchObject(value){ var myObject = data.filter(function(obj){ return obj.id == findObjectWithThisValue }); if(myObject.length == 0){ 戻り値 'undefined'; } else { return myObject; } } SearchObject(findObjectWithThisValue); –

+0

複数のマッチがある場合はどうなりますか? –

答えて

0

私はあなたがリターンにしたい場合、私は両方を行うことは非常に簡単であるとの両方を行う方法を紹介しますので、オブジェクトまたはにオブジェクトを削除するかわかりません。

これはあなたのデータの整理バージョン:

// this is your data 
var data = [{ 
    id: 1, 
    firstName: 'John', 
    lastName: 'Smith' 
}, { 
    id: 2, 
    firstName: 'Jane', 
    lastName: 'Smith' 
}, { 
    id: 3, 
    firstName: 'John', 
    lastName: 'Doe' 
}]; 

これあなたがリターン配列からターゲットオブジェクトに使用しますループ:このスニペットは、正確な処理を行い

// loop through the data array 
for(var i = 0; i < data.length; i++) { 
    // check if the current item is "John Smith" 
    if(data[i].firstName == "John" && data[i].lastName == "Smith") { 
    return data[i]; 
    } 
    // continue with the loop if the current item is not "John Smith" 
    continue; 
} 

同じものだが、無しでcontinue

// loop through the data array 
for(var i = 0; i < data.length; i++) { 
    // check if the current item is "John Smith" 
    if(data[i].firstName == "John" && data[i].lastName == "Smith") { 
    return data[i]; 
    } 
} 

これあなたが配列からターゲットオブジェクトを削除に使用しますループ:

// loop through the data array 
for(var i = 0; i < data.length; i++) { 
    // check if the current item is "John Smith" 
    if(data[i].firstName == "John" && data[i].lastName == "Smith") { 
    delete data[i]; 
    // you can also use Array.prototype.splice() to remove an item from an array: 
    // data.splice(i, 1); 
    } 
    // continue with the loop if the current item is not "John Smith" 
    continue; 
} 

このコードはまったく同じことを行いますがcontinueなし:

// loop through the data array 
for(var i = 0; i < data.length; i++) { 
    // check if the current item is "John Smith" 
    if(data[i].firstName == "John" && data[i].lastName == "Smith") { 
    delete data[i]; 
    // you can also use Array.prototype.splice() to remove an item from an array: 
    // data.splice(i, 1); 
    } 
} 

ます場合は、このスニペットを使用しますjQueryを使用していますが、jQueryのコールバック関数の中でオブジェクトを処理することはできますが、何かを返すか削除するのではなく、この場合
、私は例としてconsole.log();を使用することがあります:

$.each(data, function(i, object) { 
    if(object.firstName == "John" && object.lastName == "Smith") { 
    console.log(object); 
    } 
}); 

幸運とすべてのベスト。このリンクVIST配列の詳細を知りたい場合は

var data = [{ 
    id: 1, 
    firstName: 'John', 
    lastName: 'Smith' 
}, { 
    id: 2, 
    firstName: 'Jane', 
    lastName: 'Smith' 
}, { 
    id: 3, 
    firstName: 'John', 
    lastName: 'Doe' 
}]; 

data.find(x => {x.id === 1}); 

+0

私は配列からオブジェクトを返すとき、私はさらに操作したいので、この着信オブジェクトを受け入れる別の配列を作成する必要があります。同様にvar asf [] = function(data) –

+0

@KamranSaeedいいえ、あなたはしません。例えば ​​'console.log(object);'と書かれているところでは、それを変更してオブジェクトを関数に指定したり、必要な方法で操作することができます。 –

+0

'continue'ステートメントの目的は何ですか? –

0

バニラJS:

var findWhere = function(key,val,array) { 
    var o; 
    array.some(function(object) { return object[key] == val && (o = object); }); 
    return o; 
}; 

var data = []; //your array 
findWhere('id',1,data); //get the first object in the array where id = 1 

EDIT

は、ここで実際のコールバックを取り、ただ一つの要素を返すことができ、より良い1、または、一致するすべての要素です:

var find = function(arr,cb,all) { 
    var o; 
    if(all) { 
     return arr.filter(cb); 
    } 

    arr.some(function(object) { return cb(object) && (o = object); }); 
    return o; 
}; 

var findAll = function(arr,cb) { return find(arr,cb,true); }; 

//return the first object in the data array where the id = 1 
find(data,function(object) { return object.id == 1 }); 

//return all objects in the data array where name = 'John' 
findAll(data,function(object) { return object.firstName = 'John'; }); 
+0

これはちょうど 'find'を書こうとするのは厄介な方法です。 –

+0

より良いブラウザサポートを持つ厄介な方法です、はい – Adam

+0

@Adam私のサイトの 'find'ポリフィルをこれで置き換えてもよろしいですか?それははるかに簡単で理解しやすいです。 –

関連する問題