如何从数组中返回一个对象?

问题描述:

我有一个对象数组,我想搜索每个对象,并检查数据是否匹配给定的值,我想返回数组中的对象,如果没有找到,那么我必须返回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; 

我试图返回对象,如果条件中,如果else语句的比赛,但它给我的错误在返回数组对象。

我也试图使用_.findwhere(data, pid)它是下划线模块中的方法我可以使用它来返回对象数组?

+0

你是什么意思“回归n对象不在数组中“。你想删除它吗? – Carcigenicate

+0

var findObjectWithThisValue = 3; VAR数据= [{ ID:1, 姓: '约翰', 名字: '史密斯' },{ ID:2, 姓:'简, 名字: '史密斯' },{ id:3, 姓氏:'John', 姓氏:'Doe' }]; function SearchObject(value){ var myObject = data.filter(function(obj){ return obj.id == findObjectWithThisValue }); if(myObject.length == 0){ return'undefined'; } else { return myObject; } } SearchObject(findObjectWithThisValue); –

+0

如果有多个比赛怎么办? – 2017-02-11 18:29:48

我不知道,如果你想回报对象或删除对象,所以我会告诉你如何做到既因为它们都是很简单的事情。

这是您的数据的整理版:

// 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); 
    } 
}); 

祝你好运,万事如意。

+0

当我将对象从数组中返回时,我需要使另一个数组接受这个传入对象,因为我想进一步操作。像var asf [] =函数(数据) –

+0

@KamranSaeed不,你不知道。例如,它说'console.log(object);'你可以改变它来指定对象的任何功能或以任何你需要的方式操纵它。 – 2017-02-11 17:34:53

+0

“继续”陈述的目的是什么? – 2017-02-11 17:37:18

您可以使用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}); 

如果您想了解更多关于阵列访问次数此链接。

http://exploringjs.com/es6/ch_arrays.html

香草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 

编辑

这里有一个更好的,实际上需要一个回调,并且可以返回只有一个元素,或所有匹配的元素:

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'的一种尴尬的方式。 – 2017-02-11 17:38:15

+0

有更好的浏览器支持的尴尬的方式,是 – Adam

+0

@Adam你介意我是否用我的网站替换了我的网站上的'find'多填充?它更简单,更易于理解。 – 2017-02-11 18:33:18