创建基于字符串的具有动态属性名称的对象

时间:2019-05-03 16:36:43

标签: javascript arrays object

我有这个对象数组:

[
    {
        "id": 4,
        "user_id": 1,
        "business_id": 2,
        "branch_id": 3,
        "type": "service",
        "item": "Typing",
        "unitPrice": 100,
        "bulkUnit": 20,
        "bulkUnitPrice": 80,
        "availableUnits": "NA",
        "created_at": "2019-05-03 11:36:33",
        "updated_at": "2019-05-03 11:36:33"
    },
    {
        "id": 5,
        "user_id": 1,
        "business_id": 2,
        "branch_id": 3,
        "type": "service",
        "item": "Printing",
        "unitPrice": 70,
        "bulkUnit": 50,
        "bulkUnitPrice": 40,
        "availableUnits": "NA",
        "created_at": "2019-05-03 11:37:29",
        "updated_at": "2019-05-03 11:37:29"
    }
]

对于每个对象,我需要使用item属性的值作为新对象中的属性名称,如下所示:

{
    "Typing":  null,
    "Printing":  null
}

5 个答案:

答案 0 :(得分:1)

您可以结合使用Array.prototype.map()Object.fromEntries()

const data = [
{"id":4,"user_id":1,"business_id":2,"branch_id":3,"type":"service","item":"Typing","unitPrice":100,"bulkUnit":20,"bulkUnitPrice":80,"availableUnits":"NA","created_at":"2019-05-03 11:36:33","updated_at":"2019-05-03 11:36:33"},
{"id":5,"user_id":1,"business_id":2,"branch_id":3,"type":"service","item":"Printing","unitPrice":70,"bulkUnit":50,"bulkUnitPrice":40,"availableUnits":"NA","created_at":"2019-05-03 11:37:29","updated_at":"2019-05-03 11:37:29"}
];

const result = Object.fromEntries(data.map(x => [x.item, null]));
console.log(result)

答案 1 :(得分:1)

Array​.prototype​.reduce()

您可以在每个元素上执行化简函数并生成一个对象。

const items = [{"id":4,"user_id":1,"business_id":2,"branch_id":3,"type":"service","item":"Typing","unitPrice":100,"bulkUnit":20,"bulkUnitPrice":80,"availableUnits":"NA","created_at":"2019-05-03 11:36:33","updated_at":"2019-05-03 11:36:33"},{"id":5,"user_id":1,"business_id":2,"branch_id":3,"type":"service","item":"Printing","unitPrice":70,"bulkUnit":50,"bulkUnitPrice":40,"availableUnits":"NA","created_at":"2019-05-03 11:37:29","updated_at":"2019-05-03 11:37:29"}];
const result = items.reduce((accumulator, currentValue) => {
  accumulator[currentValue.item] = null;
  return accumulator;
}, {});

console.log(result);

答案 2 :(得分:1)

您可以通过仅将想要的属性并将此值作为计算的属性来将映射的对象分配给单个对象。

var data = [{ id: 4, user_id: 1, business_id: 2, branch_id: 3, type: "service", item: "Typing", unitPrice: 100, bulkUnit: 20, bulkUnitPrice: 80, availableUnits: "NA", created_at: "2019-05-03 11:36:33", updated_at: "2019-05-03 11:36:33" }, { id: 5, user_id: 1, business_id: 2, branch_id: 3, type: "service", item: "Printing", unitPrice: 70, bulkUnit: 50, bulkUnitPrice: 40, availableUnits: "NA", created_at: "2019-05-03 11:37:29", updated_at: "2019-05-03 11:37:29" }],
    result = Object.assign({}, ...data.map(({ item }) => ({ [item]: null })));

console.log(result);

答案 3 :(得分:0)

只需遍历所有项目,然后抓住item并将值推到新对象上即可。

const items = [
{"id":4,"user_id":1,"business_id":2,"branch_id":3,"type":"service","item":"Typing","unitPrice":100,"bulkUnit":20,"bulkUnitPrice":80,"availableUnits":"NA","created_at":"2019-05-03 11:36:33","updated_at":"2019-05-03 11:36:33"},
{"id":5,"user_id":1,"business_id":2,"branch_id":3,"type":"service","item":"Printing","unitPrice":70,"bulkUnit":50,"bulkUnitPrice":40,"availableUnits":"NA","created_at":"2019-05-03 11:37:29","updated_at":"2019-05-03 11:37:29"}
]

let result = {} 
items.forEach(i => { result[i.item] = null })
console.log(result)

答案 4 :(得分:0)

一种非常简单的方法:    

   <script>
 var oldJSON = '[{"id":4,"user_id":1,"business_id":2,"branch_id":3,"type":"service","item":"Typing","unitPrice":100,"bulkUnit":20,"bulkUnitPrice":80,"availableUnits":"NA","created_at":"2019-05-03 11:36:33","updated_at":"2019-05-03 11:36:33"},{"id":5,"user_id":1,"business_id":2,"branch_id":3,"type":"service","item":"Printing","unitPrice":70,"bulkUnit":50,"bulkUnitPrice":40,"availableUnits":"NA","created_at":"2019-05-03 11:37:29","updated_at":"2019-05-03 11:37:29"}]';

 // Convert to object
 var oldJSONObj = JSON.parse(oldJSON);

 var newJSONObj = {}

 // Loop through & get the value for item and add it to new JSON  
 for (var key in oldJSONObj) {
   if (oldJSONObj.hasOwnProperty(key)) {
     var val = oldJSONObj[key]["item"];
     newJSONObj[val] = null;
   }
 }
 //newJSONObj contains the JSON you require

 document.write(JSON.stringify(newJSONObj));

   </script>