Home > Software design >  Transform object to array of object inside of array object
Transform object to array of object inside of array object

Time:09-14

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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)

CodePudding user response:

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 }

CodePudding user response:

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(Object.assign({}, ar[ar.indexOf(e)][x]))
      delete ar[ar.indexOf(e)][x]
}    
}
})
const obj = {...ar , temp}
return obj
}
console.log(func(items))

  • Related