0

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
}

5 Answers 5

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

You need to explain your code, please don't post just the code on Stackoverflow.
0

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)

Comments

0

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

Comments

0

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>

   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.