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

时间:2016-01-29 16:54:37

标签: javascript arrays

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

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

要提取具有name: "CHRISTMAS"的对象,我使用filter:

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

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

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

toDoHoliday作为[{name: "CHRISTMAS"}]返回,因此仍然是一个包含一个对象的数组。

如何将我的对象仅仅作为一个对象,所以我可以在以后做toDoHoliday.name之类的事情等等?

7 个答案:

答案 0 :(得分:3)

使用[n]表示法从n

返回的对象数组中选择索引.filter()处的项目
  var holidayIn = "CHRISTMAS";
  var toDoHoliday = holidayList.filter(function(x){
    return x.name === holidayIn;
  });
  console.log(toDoHoliday[0].name)

答案 1 :(得分:1)

您可以使用Array#find

  

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

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

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

答案 2 :(得分:1)

如果您对第三方库感到满意,则无法通过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之外的许多其他事情,它当然是有用的。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

最好的方法是首先迭代你的阵列 所以,在这里输入代码

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

答案 5 :(得分:0)

另一种方法是使用ES6 destructuring

      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]

答案 6 :(得分:0)

另一种方法是使用ES6 destructuring

&#13;
&#13;
      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) 
&#13;
&#13;
&#13;