从长对象中删除元素

时间:2018-03-25 09:41:49

标签: javascript css

我有一个看起来像这样的对象:

[{"1":{"name":"A","email":2}},
{"2":{"name":"B","email":3}},
{"3":{"name":"C","email":4}},]
{"4":{"name":"B","email":5}}]

我想得到如下结果:

 [{"name":"A","email":2}},
   {"name":"B","email":3}},
   {"name":"C","email":4}},]
   {"name":"B","email":5}}]

我试过但是这段代码不起作用,现在我对如何为此解决方案感到有点迷茫。

obj.map(a => {
  var newobj
   a = []
   a.push[a]
})

2 个答案:

答案 0 :(得分:6)

你需要阅读Array.prototype.map(),你使用它的方式不正确

 var homework =[{"1":{"name":"A","email":2}},
                  {"2":{"name":"B","email":3}},
                  {"3":{"name":"C","email":4}},
                  {"4":{"name":"B","email":5}}];

var outputofthehomework = homework.map( s => Object.values(s)[0] );
console.log(outputofthehomework)

答案 1 :(得分:3)

var a = [{"1":{"name":"A","email":2}},{"2":{"name":"B","email":3}}, {"3":{"name":"C","email":4}}, {"4":{"name":"B","email":5}}];

var b = [];   
a.forEach(function(element){
    for (var key in element){
          b.push(element[key]);
    }      
});

console.log(b)