私はあなたがリターンにしたい場合、私は両方を行うことは非常に簡単であるとの両方を行う方法を紹介しますので、オブジェクトまたはがにオブジェクトを削除するかわかりません。
これはあなたのデータの整理バージョン:
// 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});
:
「retur n個のオブジェクトが配列から外れています "。それを削除しますか? – Carcigenicate
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); –
複数のマッチがある場合はどうなりますか? –