Javascript 如何将对象数组转换为对象数组

Javascript 如何将对象数组转换为对象数组,javascript,arrays,object,redux,Javascript,Arrays,Object,Redux,我有一个单一对象的数组,其中有一些对象 我要删除第一个对象并将所有对象分散到阵列中 我有这个 friends: [ { one: { id: 'one', address: 'something' }, two: { id: 'two', roomID: 'something' }, me: 'three' } ] 我想要这个 fr

我有一个单一对象的数组,其中有一些对象 我要删除第一个对象并将所有对象分散到阵列中 我有这个

friends: [
      {
    one: {
          id: 'one',
          address: 'something'
        },
   two: {
          id: 'two',
          roomID: 'something'
        },
   me: 'three'
      }
    ]
我想要这个

friends: [
        {
          id: 'one',
          address: 'something'
        },
         {
          id: 'two',
          roomID: 'something'
        },
    ]

这是一个快速的例子

Object.keys(friends[0]).map(key => Object.prototype.toString.call(friends[0][key]) === '[object Object]' ? friends[0][key] : null).filter(x => x)

当你想过滤掉数据,只得到里面的对象时,你可以试试这个

Object.values(req).filter(ele=>{
    if((ele instanceof Object) && !(ele instanceof Array)){
        return ele
    }
})
其中
req
friends[0]
from

friends: [
      {
    one: {
          id: 'one',
          address: 'something'
        },
   two: {
          id: 'two',
          roomID: 'something'
        },
   me: 'three'
      }
    ]

由于您希望
map
仅映射属性
one
two
,因此我们可以将它们用于
flatMap
方法的参数中,以获得所需的结果:

const result = friends.flatMap(({one, two}) =>([{...one}, {...two}]));
例如:

let friends=[
{
一:{
id:'一',
地址:某物
},
二:{
id:'两个',
室友:“有东西”
},
我:“三”
}
];
const result=friends.flatMap(({one,two})=>([{…one},{…two}]);

控制台日志(结果)
data.friends=Object.values(data.friends[0]).filter(el=>el.id)