AsZik
AsZik

Reputation: 641

push the array value to object

I have a nested array . I want to take values from array and push to new single object. should read take the _id as object key and category array field should be value for _id

const Ll = [
  {
    _id: 'milk',
    category: [
      [
        {
          name: 'Alfred',
          job: 'manager'
        },
        {
          name: 'Mark',
          job: 'manager'
        }
      ]
    ]
  },
  {
    _id: 'grocery',
    category: [
      [
        {
          name: 'William',
          job: 'manager'
        }
      ]
    ]
  }
]

I want object like so,

const obj = {
  milk: [
    {
      name: 'Alfred',
      job: 'manager'
    },
    {
      name: 'Mark',
      job: 'manager'
    }
  ],
  grocery: [
    {
      name: 'William',
      job: 'manager'
    }
  ]
}

Is possible to do Thanks!

Upvotes: 0

Views: 72

Answers (4)

hgb123
hgb123

Reputation: 14881

You could use reduce with empty object accumulated

Technique combined with:

const data = [ { _id: "milk", category: [ [ { name: "Alfred", job: "manager", }, { name: "Mark", job: "manager", }, ], ], }, { _id: "grocery", category: [ [ { name: "William", job: "manager", }, ], ], }, ];

const res = data.reduce(
  (acc, { _id, category }) => ({ ...acc, [_id]: category.flat() }),
  {}
);

console.log(res);

Upvotes: 1

Steven.J
Steven.J

Reputation: 23

Here is solution for you.

const obj = {};
data.forEach(d => {
    const categories = d.category.reduce((a, v) => a.concat(v), []);
    obj[d._id] = obj[d._id] ? [...obj[d._id], ...categories] : [...categories];
});

console.log(obj);

Upvotes: 1

Vasile Radeanu
Vasile Radeanu

Reputation: 906

const keys = [...new Set(data.map(item => item._id))];

const newObj = keys.reduce((acc, curr) => {
  const value = data.filter(({ _id }) => _id === curr).map(({ category }) => {
    return {
      ...category[0][0]
    }
  });

  return {
    ...acc,
    [curr]: value
  }
}, {});

Upvotes: 0

phi-rakib
phi-rakib

Reputation: 3302

You could do it using Array.prototype.reduce() method. Traverse the array and group it by _id.

const data = [
  {
    _id: 'milk',
    category: [
      [
        {
          name: 'Alfred',
          job: 'manager',
        },
      ],
    ],
  },
  {
    _id: 'grocery',
    category: [
      [
        {
          name: 'William',
          job: 'manager',
        },
      ],
    ],
  },
  {
    _id: 'milk',
    category: [
      [
        {
          name: 'Mark',
          job: 'manager',
        },
      ],
    ],
  },
];
const ret = data.reduce((prev, c) => {
  const p = prev;
  const key = c._id;
  p[key] = p[key] ?? [];
  p[key].push(...c.category.flat());
  return p;
}, {});
console.log(ret);

ES6:

const data = [
  {
    _id: 'milk',
    category: [
      [
        {
          name: 'Alfred',
          job: 'manager',
        },
      ],
    ],
  },
  {
    _id: 'grocery',
    category: [
      [
        {
          name: 'William',
          job: 'manager',
        },
      ],
    ],
  },
  {
    _id: 'milk',
    category: [
      [
        {
          name: 'Mark',
          job: 'manager',
        },
      ],
    ],
  },
];
const ret = data.reduce((prev, c) => {
  const p = prev;
  const key = c._id;
  p[key] = p[key] || [];
  p[key].push(...c.category.reduce((acc, val) => acc.concat(val), []));
  return p;
}, {});
console.log(ret);

Upvotes: 2

Related Questions