Home > Net >  Check object for duplicate id and combine into new object
Check object for duplicate id and combine into new object

Time:12-14

I have an array like the picture, now i want to check if the id is duplicate then combine it and the value of "key" will become key with the value is 1 or 0

enter image description here

final result :

[
  {
       id: 4,
       view: 1,
       add: 1,
       edit: 1,
       delete: 0,
  },
  {
       id: 7,
       view: 1,
       add: 1,
       edit: 0,
       delete: 0,
   },
];

code

const App = () => {
    const arr = [
      { id: 4, key: "view" },
      { id: 4, key: "add" },
      { id: 7, key: "view" },
      { id: 7, key: "add" },
      { id: 4, key: "edit" }
    ];

    arr.map((item,index) => {
       // check the next id is exists
       if(arr[index   1] && item.id === arr[index   1].id) {
           //
       }
    }) 

  return (
    <div></div>
  );
};

CodePudding user response:

  1. create a object template which initialises all the values to zero.
  2. reduce over the array of objects. If the id is not found on the initial object as a key add it, and set its value to an object containing the id, and a merged copy of template.
  3. Set any found keys to 1.
  4. Grab the resulting object's values to get an array of objects matching your expected output.

const arr = [
  { id: 4, key: "view" },
  { id: 4, key: "add" },
  { id: 7, key: "view" },
  { id: 7, key: "add" },
  { id: 4, key: "edit" }
];

// Create an object template
const tmpl = { view: 0, add: 0, edit: 0, delete: 0 };

// `reduce` over the array of objects. If the id
// doesn't exist on the initialised object as a key
// add it and set it's value to an object containing
// the id, and a copy of the object template, and
// then update any keys that are found.
const obj = arr.reduce((acc, c) => {
  const { id, key } = c;
  acc[id] ??= { id, ...tmpl };
  acc[id][key] = 1;
  return acc;
}, {});

console.log(Object.values(obj));

Additional documentation

CodePudding user response:

const data = [
    { id: 4, key: "view" },
    { id: 4, key: "add" },
    { id: 7, key: "view" },
    { id: 7, key: "add" },
    { id: 4, key: "edit" }
]

const result = data.reduce((p, c) => {
    const found = p.findIndex(p => p.id === c.id);
    if (found !== -1) {
        p[found][c.key] = 1;
    } else {
        const tmpObj = {
            id: c.id,
            view: 0,
            add: 0,
            edit: 0,
            delete: 0
        }
        tmpObj[c.key] = 1;
        p.push(tmpObj)
    }

    return p;
}, []);

console.log(result);

CodePudding user response:

const arr = [
      { id: 4, key: "view" },
      { id: 4, key: "add" },
      { id: 7, key: "view" },
      { id: 7, key: "add" },
      { id: 4, key: "edit" }
 ];
const r = arr.reduce((c, d, i) => {
  const {
    id,
    key
  } = d;
  if (c[id]) {
    c[id][key] = 1
  } else {
    c[id] = {
      id,
      view: 0,
      edit: 0,
      add: 0,
      delete: 0
    }
    c[id][key] = 1
  }
  return c
}, {});

const result = Object.values(r);

console.info(result)

  • Related