1

My object currently look like this. I'd like to transform the item object into an array of object.

const items = [
 {
 item_id: 1,
 stock_on_hand: 400,
 item: {
 id: 1,
 code: "ITEM 1",
 },
 },
 {
 item_id: 2,
 stock_on_hand: 150,
 item: {
 id: 2,
 code: "ITEM 2",
 },
 },
];

This result I want to output:

const items = {
 0: {
 item_id: 1,
 stock_on_hand: 400,
 },
 1: {
 item_id: 2,
 stock_on_hand: 150,
 },
 item: [
 {
 id: 1,
 code: "ITEM 1",
 },
 {
 id: 2,
 code: "ITEM 2",
 },
 ],
};

What I've tried:But the item object is still inside the object I want to remove them.

const arr = [];
for(const [key, value] of Object.entries(items)) {
 arr.push(Object.assign({}, value.item));
}
const item = {...items, arr};
asked Sep 14, 2022 at 6:05

6 Answers 6

1

Just delete the item from the value

const arr = [];
for(const [key, value] of Object.entries(items)) {
 arr.push(Object.assign({}, value.item));
 delete value.item;
}
const item = {...items, arr};
answered Sep 14, 2022 at 6:11
Sign up to request clarification or add additional context in comments.

Comments

1

You can use forEach to accomplish it, like this:

const items = [
 {
 item_id: 1,
 stock_on_hand: 400,
 item: {
 id: 1,
 code: "ITEM 1",
 },
 },
 {
 item_id: 2,
 stock_on_hand: 150,
 item: {
 id: 2,
 code: "ITEM 2",
 },
 },
];
const obj = {};
items.forEach((el, idx) => {
 const {item, ...otherProps} = el;
 obj[idx] = otherProps;
 obj.items ??= [];
 obj.items.push(item);
})
console.log(obj);

answered Sep 14, 2022 at 6:12

Comments

0

Actually you can use .reduce and destructuring to transform an array

const items = [{item_id: 1,stock_on_hand: 400,item: {id: 1,code: "ITEM 1",},},{item_id: 2,stock_on_hand: 150,item: {id: 2,code: "ITEM 2",},},];
const result = items.reduce((acc, { item, ...rest }) => {
 acc[rest.item_id] = rest;
 acc.item.push(item);
 return acc;
}, { item: [] });
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }

answered Sep 14, 2022 at 6:42

Comments

0

You can't technically do exactly what you want because you cannot use integers as properties of an object (they get converted to strings). You can get close though:

const items = [
 {
 item_id: 1,
 stock_on_hand: 400,
 item: {
 id: 1,
 code: "ITEM 1",
 },
 },
 {
 item_id: 2,
 stock_on_hand: 150,
 item: {
 id: 2,
 code: "ITEM 2",
 },
 },
];
let result = items.reduce( (accum, current, index) => {
 accum[index] = (({ item_id, stock_on_hand }) => ({item_id, stock_on_hand}))(current)
 accum.item.push( current.item )
 return accum
}, { item: [] })
console.log(result)

adiga
35.4k9 gold badges66 silver badges88 bronze badges
answered Sep 14, 2022 at 6:26

Comments

0

You can simply achieve this by using Array.forEach() method.

Live Demo :

const items = [
 {
 item_id: 1,
 stock_on_hand: 400,
 item: {
 id: 1,
 code: "ITEM 1",
 },
 },
 {
 item_id: 2,
 stock_on_hand: 150,
 item: {
 id: 2,
 code: "ITEM 2",
 },
 },
];
const obj = {};
items.forEach((itemObj, index) => {
 (obj.item) ? obj.item.push(itemObj.item) : obj.item = [itemObj.item]
 delete itemObj.item;
 obj[index] = itemObj;
});
console.log(obj);

answered Sep 14, 2022 at 14:43

Comments

0

we can get this way without metion key access

const items = [
 { item_id: 1, stock_on_hand: 400, item: { id: 1, code: "ITEM 1"}},
 { item_id: 2, stock_on_hand: 150, item: { id: 2, code: "ITEM 2"}},
 ];
const func =(ar)=>{
 const temp = []
 ar.map((e, i)=> {
 for(let [x, ind] of Object.entries(e)){
 if(typeof ind == "object"){
 temp.push(ar[ar.indexOf(e)][x])
 delete ar[ar.indexOf(e)][x]
} 
}
})
const obj = {...ar , temp}
return obj
}
console.log(func(items))

answered Sep 14, 2022 at 8:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.