我如何 return 数组中的对象?
How can I return an object out of an array?
我有一个对象数组,我想搜索每个对象并检查数据是否与给定值匹配,我想 return 从数组中删除对象,如果找不到则我必须 return undefined.
这是我目前拥有的:
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;
如果 if else 语句中的条件匹配,我正在尝试 return 对象,但它在 returning 数组对象中给我错误。
我也在尝试使用 _.findwhere(data, pid)
这是下划线模块中的方法我可以用它来 return 数组外的对象吗?
我不确定你是想return对象还是删除对象,所以我会展示你怎么做,因为两者都很简单。
这是您数据的整理版本:
// this is your data
var data = [{
id: 1,
firstName: 'John',
lastName: 'Smith'
}, {
id: 2,
firstName: 'Jane',
lastName: 'Smith'
}, {
id: 3,
firstName: 'John',
lastName: 'Doe'
}];
这是您将用于 return 数组中目标对象的循环:
// 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,请使用此代码段,而不是 return 创建或删除任何您可以在 jQuery 回调函数中处理对象的内容。
在这种情况下,我将使用 console.log();
作为示例:
$.each(data, function(i, object) {
if(object.firstName == "John" && object.lastName == "Smith") {
console.log(object);
}
});
祝你好运,万事如意。
您可以使用 Array.prototype.find()
,像这样:
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});
如果您想了解更多关于数组的信息,请访问此 link。
香草 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
编辑
这里有一个更好的回调函数,它可以 return 只有一个元素,或者所有匹配的元素:
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'; });
我有一个对象数组,我想搜索每个对象并检查数据是否与给定值匹配,我想 return 从数组中删除对象,如果找不到则我必须 return undefined.
这是我目前拥有的:
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;
如果 if else 语句中的条件匹配,我正在尝试 return 对象,但它在 returning 数组对象中给我错误。
我也在尝试使用 _.findwhere(data, pid)
这是下划线模块中的方法我可以用它来 return 数组外的对象吗?
我不确定你是想return对象还是删除对象,所以我会展示你怎么做,因为两者都很简单。
这是您数据的整理版本:
// this is your data
var data = [{
id: 1,
firstName: 'John',
lastName: 'Smith'
}, {
id: 2,
firstName: 'Jane',
lastName: 'Smith'
}, {
id: 3,
firstName: 'John',
lastName: 'Doe'
}];
这是您将用于 return 数组中目标对象的循环:
// 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,请使用此代码段,而不是 return 创建或删除任何您可以在 jQuery 回调函数中处理对象的内容。
在这种情况下,我将使用 console.log();
作为示例:
$.each(data, function(i, object) {
if(object.firstName == "John" && object.lastName == "Smith") {
console.log(object);
}
});
祝你好运,万事如意。
您可以使用 Array.prototype.find()
,像这样:
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});
如果您想了解更多关于数组的信息,请访问此 link。
香草 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
编辑
这里有一个更好的回调函数,它可以 return 只有一个元素,或者所有匹配的元素:
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'; });