skip to Main Content
const array1 = [
  {
    id: "40",
    alias: "Number",
    name: "Number",
  },
  {
    id: "41",
    alias: "Salary",
    name: "Salary",
  },
];


const array2 = [
  {
    id: "Sum__Salary__U3VtKFVTJTIwRW1wbG95ZWUuU2FsYXJ5KQ__",
    name: "Salary",
  },
  { id: "40", name: "Number" },
];

I want to modify my array2, my ‘Salary’ id should become "41" in my second array instead of "Sum__Salary__U3VtKFVTJTIwRW1wbG95ZWUuU2FsYXJ5KQ__"

Sharing the snipped which i have tried:

const result = array1
  .filter((array1Value) =>
    [...array2].find(
      (array2Value) => array2Value.name === array1Value.alias
    )
  )
  .map((column) => ({
    id: column.id,
    name: column.name,
  }));
console.log("result: ", result);

The above snippet is working fine, but here I am returning new set of array using map, is there any way where I can replace the values instead of creating new set of array? Looking for good approach to implement this.

4

Answers


  1. You can use forEach Method:

    array2.forEach(data2 => {
        array1.forEach(data1 => {
            if (data2.name == data1.alias) data2.id = data1.id;
        })
    })
    
    const array1 = [{
        id: "40",
        alias: "Number",
        name: "Number",
      },
      {
        id: "41",
        alias: "Salary",
        name: "Salary",
      },
    ];
    
    
    const array2 = [{
        id: "Sum__Salary__U3VtKFVTJTIwRW1wbG95ZWUuU2FsYXJ5KQ__",
        name: "Salary",
      },
      {
        id: "40",
        name: "Number"
      },
    ];
    
    console.log("Before :");
    console.log(array2);
    
    array2.forEach(data2 => {
      array1.forEach(data1 => {
        if (data2.name == data1.alias) data2.id = data1.id;
      })
    });
    
    console.log("After :");
    console.log(array2);
    Login or Signup to reply.
  2. Here is the solution using forEach & find high order functions:

    const array1 = [
      {
        id: "40",
        alias: "Number",
        name: "Number",
      },
      {
        id: "41",
        alias: "Salary",
        name: "Salary",
      },
    ];
    
    const array2 = [
      {
        id: "Sum__Salary__U3VtKFVTJTIwRW1wbG95ZWUuU2FsYXJ5KQ__",
        name: "Salary",
      },
      { id: "40", name: "Number" },
    ];
    
    array1.forEach((item1) => {
      const matchingItem = array2.find((item2) => item2.name === item1.alias);
      if (matchingItem) {
        matchingItem.id = item1.id;
      }
    });
    
    console.log("Modified array2:", array2);
    Login or Signup to reply.
  3. As a one-liner:

    a2.forEach(x => x.id = a1.find((y) => x.name===y.name)?.id ?? x.id);
    
    const a1 = [{
        id: "40",
        alias: "Number",
        name: "Number",
      },
      {
        id: "41",
        alias: "Salary",
        name: "Salary",
      },
    ];
    
    
    const a2 = [{
        id: "Sum__Salary__U3VtKFVTJTIwRW1wbG95ZWUuU2FsYXJ5KQ__",
        name: "Salary",
      },
      {
        id: "40",
        name: "Number"
      },
    ];
    
    a2.forEach(x => x.id = a1.find((y) => x.name===y.name)?.id ?? x.id);
    
    console.log(a2);
    Login or Signup to reply.
  4. const temp = JSON.parse(JSON.stringify(array2))
    array2.forEach((elem, index) => {
        console.log(elem?.name)
        for (let item of array1) {
            if (item.name === elem.name && item.id !== elem.id) {
                temp[index]['id'] = item.id
            }
        }
    })
    console.log(temp)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search