如何从数组中返回对象? [重复]

问题描述 投票:0回答:3

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

这是我到目前为止所拥有的:

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 语句中的条件匹配,我尝试返回对象,但它在返回数组对象时给出错误。

我也尝试使用

_.findwhere(data, pid)
,这是下划线模块中的方法,我可以用它从数组中返回对象吗?

javascript node.js underscore.js
3个回答
1
投票

您可以使用

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


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];
  }
}

这是您将用来从数组中remove目标对象的循环:

// 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
投票

原版 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'; });
© www.soinside.com 2019 - 2024. All rights reserved.