suleiman abdulrazaq
suleiman abdulrazaq

Reputation: 77

Create object with dynamic property names based on strings

I have this array of objects:

[
    {
        "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"
    }
]

For each object, I need to use the value of the item property as a property name in a new object, like this:

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

Upvotes: 0

Views: 85

Answers (5)

Trupti Shetty
Trupti Shetty

Reputation: 66

A very simple way of doing it:

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

   

Upvotes: 0

Chieh
Chieh

Reputation: 33

Array​.prototype​.reduce()

You can executes the reducer function on each element and resulting in an object.

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

Upvotes: 1

arkeros
arkeros

Reputation: 177

You can achieve this using a combination of Array.prototype.map() and 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)

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386570

You could assign the mapped objects to a single object by taking only the wanted property and tak this value as computed property.

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

Upvotes: 0

Get Off My Lawn
Get Off My Lawn

Reputation: 36311

Just loop over the items, and grab the item and push the value onto a new object:

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)

Upvotes: 0

Related Questions