Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从数组中返回对象?_Javascript_Node.js_Underscore.js - Fatal编程技术网

Javascript 如何从数组中返回对象?

Javascript 如何从数组中返回对象?,javascript,node.js,underscore.js,Javascript,Node.js,Underscore.js,我有一个对象数组,我想搜索每个对象,检查数据是否与给定值匹配,我想从数组中返回对象,如果找不到,则必须返回未定义的对象 这就是我到目前为止所做的: var data = [{ id: 1, firstName: 'John', lastName: 'Smith' }, { id: 2, firstName: 'Jane', lastName: 'Smith' }, { id: 3, firstName: 'John', lastName: 'Doe' }];

我有一个对象数组,我想搜索每个对象,检查数据是否与给定值匹配,我想从数组中返回对象,如果找不到,则必须返回未定义的对象

这就是我到目前为止所做的:

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语句中的条件匹配,我将尝试返回该对象,但返回数组对象时会出现错误


我还尝试使用
这是下划线模块中的方法,我可以使用它从数组中返回对象吗?

我不确定您是想返回对象还是删除对象,因此我将向您演示如何执行这两个操作,因为这两个操作都非常简单

这是您的数据的整理版本:

// 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;
}
// 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;
}
这是用于从阵列中删除目标对象的循环:

// 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;
}
// 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;
}
如果您正在使用jQuery,请使用此代码段,而不是在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});
如果您想了解更多有关阵列的信息,请访问此链接

香草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'; });

“从数组中返回对象”是什么意思。是否要删除它?var findObjectWithThisValue=3;var data=[{id:1,firstName:'John',lastName:'Smith'},{id:2,firstName:'Jane',lastName:'Smith'},{id:3,firstName:'John',lastName:'Doe'}];函数SearchObject(value){var myObject=data.filter(函数(obj){return obj.id==findObjectWithThisValue});if(myObject.length==0){return'undefined';}else{return myObject;}}SearchObject(findObjectWithThisValue);如果有多个匹配项呢?当我从数组中返回对象时,我需要创建另一个数组来接受这个传入对象,因为我想进一步操作它。像var asf[]=function(data)@KamranSaeed不,你没有。例如,它说
console.log(对象)
您可以将其更改为将对象指定为任何函数,或以您需要的任何方式对其进行操作。
continue
语句的目的是什么?@torazaburo向OP显示continue语句是可选的,不是必需的。如果我只想根据lastname进行搜索,该怎么办。在这种情况下,它必须从数组中返回两个对象。那么如何做到这一点。因为如果我使用return语句,它会在第一次匹配时返回。这只是一种编写
find
的笨拙方式。这种笨拙的方式有更好的浏览器支持,yes@Adam你介意我用这个替换我网站上的
find
poly fill吗?它更简单,更容易理解。@Mango-当然,但第一个例子非常有限。请参阅我的编辑,它采用回调函数(如实际的
数组.prototype.find
方法),可以轻松地用于仅返回第一个元素或所有匹配元素,这远远超出了仅在具有特定键值对的数组中查找对象的范围。@Adam谢谢,mate!你真的让我高兴。