将单个对象提取为对象,而不是具有一个对象的数组,Javascript

Extracting a single object as an object rather than as an array with one object, Javascript

本文关键字:对象 一个对象 数组 Javascript 提取 单个      更新时间:2023-09-26

我有一个对象数组,只想提取一个对象,但稍后将该对象作为对象而不是包含一个对象的数组进行操作。我的对象数组是:

  var holidayList = [
    {name: "CHRISTMAS",
    month: 11,
    modifier: 5},{...},{...},
  ]

例如,要提取带有name: "CHRISTMAS"的对象,我使用过滤器:

  var holidayIn = "CHRISTMAS";
  var toDoHoliday = holidayList.filter(function(x){
    return x.name === holidayIn;
  });

现在,如果我console.log(toDoHoliday);,我会得到一个[{name: "CHRISTMAS", month: 11, modifier: 5}]一个包含一个对象的数组。我尝试使用地图:

  var toDoHoliday = holidayList.filter(function(x){
    return x.name === holidayIn;
  }).map(function(element){
    return {name: element.name};
  });

它返回toDoHoliday[{name: "CHRISTMAS"}],所以仍然是一个包含一个对象的数组。

如何才能将我的对象作为一个对象,以便以后可以执行诸如toDoHoliday.name之类的操作?

使用[n]表示法从 .filter() 返回的对象数组中选择索引 n 处的项目

  var holidayIn = "CHRISTMAS";
  var toDoHoliday = holidayList.filter(function(x){
    return x.name === holidayIn;
  });
  console.log(toDoHoliday[0].name)
您可以使用

Array#find

如果数组中的元素满足提供的测试函数,则 find() 方法返回数组中的值。否则,将返回undefined

var toDoHoliday = holidayList.find(function(x){
  return x.name === holidayIn;
});

注意:并非所有浏览器都支持它,因此如果代码在浏览器中运行,则需要填充它。

如果你对第三方库没问题,你就不能跳过lodash(自切片面包以来最有用的东西之一),在你的特殊情况下,不能跳过_.find函数。

从文档中:

var users = [
  { 'user': 'barney',  'age': 36, 'active': true },
  { 'user': 'fred',    'age': 40, 'active': false },
  { 'user': 'pebbles', 'age': 1,  'active': true }
];
_.find(users, function(o) { return o.age < 40; });
// → object for 'barney'
// using the `_.matches` iteratee shorthand
_.find(users, { 'age': 1, 'active': true });
// → object for 'pebbles'
// using the `_.matchesProperty` iteratee shorthand
_.find(users, ['active', false]);
// → object for 'fred'
// using the `_.property` iteratee shorthand
_.find(users, 'active');
// → object for 'barney'

当然,除了简单的_.find之外,它还对许多其他事情很有用。

var holidayIn = "CHRISTMAS";
var toDoHoliday = null; // Will remain as this if not found
for ( var i = 0; i < holidayList.length; i++ ){
  if ( holidayList[i].name === holidayIn ){
    toDoHoliday = holidayList[i];
    break;
  }
}

最好的方法是先迭代数组。
因此,在此处输入代码

var cnt = holidayList.length();  
for(var i = 0; i < cnt; i++){  
   var newObject = holidayList[$i]; 
   //And now you can do newObject.name etc  
}

另一种方法是使用 ES6 解构

      var holidayIn = "CHRISTMAS";                                                               
      var holidayList = [       
         {name: "CHRISTMAS" , month: 11, modifier: 5},
         {name: "CHRISTMAS1", month: 12, modifier: 65},
         {name: "CHRISTMAS2", month: 144,modifier: 77},
       ]                                                                                         
     let [toDoHoliday] = holidayList.filter((x)=>{     
       return x.name === holidayIn                     
     })                                                                                              
     console.log(toDoHoliday) 
     console.log(toDoHoliday.name) 

[![destructuring][1]][1]

另一种方法是使用 ES6 解构

      var holidayIn = "CHRISTMAS";                                                               
      var holidayList = [       
         {name: "CHRISTMAS" , month: 11, modifier: 5},
         {name: "CHRISTMAS1", month: 12, modifier: 65},
         {name: "CHRISTMAS2", month: 144,modifier: 77},
       ]                                                                                         
     let [toDoHoliday] = holidayList.filter((x)=>{     
       return x.name === holidayIn                     
     })                                                                                              
     console.log(toDoHoliday) 
     console.log(toDoHoliday.name)