将对象数组转换为具有特定值对的单个对象

时间:2018-03-22 02:42:47

标签: javascript

我知道有很多关于如何将数组转换为对象的问题我知道你可以做这样的事情...... from bitarray import bitarray class id_allocator(): def __init__(self, size): self.ids = bitarray(size) # 0 is available, 1 is unavailable def alloc(self): for idx, _id in enumerate(self.ids): if not _id: self.ids[idx] = True return idx raise Exception('no ids available') def release(self, _id): if _id > len(self.ids) or self.ids[_id] == True: raise Exception('invalid id') self.ids[_id] = False 但我的问题更具体

说我有一系列像这样的对象..

object = {...arr};

我想创建一个看起来像这样的对象..

[
    {
       dataType: something,
       dataValue: book
    },
    {
       dataType: food,
       dataValue: brocolli
    },
    {
       dataType: object,
       dataValue: chair
    },
]

我试着这样做......

{
   something: book,
   food: brocolli,
   object: chair
}

但是没有用..任何帮助都会受到赞赏!

6 个答案:

答案 0 :(得分:4)

您可以使用reduce



var arr = [{
    dataType: 'something',
    dataValue: 'book'
  },
  {
    dataType: 'food',
    dataValue: 'brocolli'
  },
  {
    dataType: 'object',
    dataValue: 'chair'
  },
]

var obj = arr.reduce((c, {dataType,dataValue}) => Object.assign(c, {[dataType]: dataValue}), {});

console.log(obj);




答案 1 :(得分:3)

您可以使用reduce功能。



var array = [    {       dataType: 'something',       dataValue: 'book'    },    {       dataType: 'food',       dataValue: 'brocolli'    },    {       dataType: 'object',       dataValue: 'chair'    },];

var result = array.reduce((a, {dataType, dataValue}) => {
  a[dataType] = dataValue;
  return a;
}, {});

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




使用Spread语法和计算属性名称。



var array = [    {       dataType: 'something',       dataValue: 'book'    },    {       dataType: 'food',       dataValue: 'brocolli'    },    {       dataType: 'object',       dataValue: 'chair'    },];

var result = array.reduce((a, {dataType, dataValue}) => ({...a, [dataType]: dataValue}), {});

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 2 :(得分:2)

一些Array.prototype.reduce(),解构和传播完成了这项工作:

var a = [
    {
       dataType: 'something',
       dataValue: 'book'
    },
    {
       dataType: 'food',
       dataValue: 'brocolli'
    },
    {
       dataType: 'object',
       dataValue: 'chair'
    },
];

// ES6
var new6 = a.reduce((o, {dataType, dataValue}) => ({...o, [dataType]: dataValue}), {});
console.log(new6);

//ES5
var new5 = a.reduce(function(o, i) {
  var prop = {}; prop[i.dataType] = i.dataValue;
  return Object.assign(o, prop); 
}, {});
console.log(new5);

//ES3.1 (lol)
var new31 = {}, i;
for (i = 0; i < a.length; i++) {
  new31[a[i].dataType] = a[i].dataValue;
}
console.log(new31);

答案 3 :(得分:1)

喜欢这个吗?

arr.reduce((acc,val)=>{acc[val.dataType]=val.dataValue;return acc;},{})

答案 4 :(得分:1)

您可能希望使用newarray[item.dataType] = item.dataValue;设置将这些键值对设置为新阵列。

var arr = [
        {
           dataType: 'something',
           dataValue: 'book'
        },
        {
           dataType: 'food',
           dataValue: 'brocolli'
        },
        {
           dataType: 'object',
           dataValue: 'chair'
        },
    ];
    var newarray = {};
    arr.forEach(item => {
        newarray[item.dataType] = item.dataValue;
    });
    newObject = {...newarray};

    console.log(newObject);

答案 5 :(得分:0)

作为reduce的替代方案,您可以使用Object.assign()Array.map()。除了使用对象分配外,这与您的原始尝试非常相似。我更喜欢这种方法,因为传播只在创建映射后执行一次。我的直觉告诉我这比reduce更有效,但我还没有经过测试。

&#13;
&#13;
var arr = [{
    dataType: 'something',
    dataValue: 'book'
  },
  {
    dataType: 'food',
    dataValue: 'brocolli'
  },
  {
    dataType: 'object',
    dataValue: 'chair'
  },
]

var obj = Object.assign({}, ...arr.map(({ dataType, dataValue }) => ({ [dataType]: dataValue })))

console.log(obj);
&#13;
&#13;
&#13;