JavaScript map id from one array of object to another

Ashok

const rowData = [];
const arr1 = [{
    "bId": "F1.1.4",
    "bName": "Mercedes",
    "cId": "150494",
    "cName": "Benz",
    "flag": "Maintain"
  },
  {
    "bId": "F1.1.4",
    "bName": "Ford",
    "cId": "150744",
    "cName": "Mustang",
    "flag": "Maintain"
  },
  {
    "bId": "F1.1.4",
    "bName": "Volkswagon",
    "cId": "4961",
    "cName": "Polo",
    "flag": "Maintain"
  },
  {
    "bId": "F1.1.5",
    "bName": "Isdera",
    "cId": "N/A",
    "cName": "Isdera",
    "flag": "Test"
  }
]

const arr2 = [{
    "cId": "150494",
    "flag": "Decommission"
  },
  {
    "cId": "150744",
    "flag": "Invest"
  },
  {
    "cId": "4961",
    "flag": "Pending"
  }
]

for (let i = 0; i < arr1.length; i++) {
  const obj = {
    custId: arr1[i].cId,
    custName: arr1[i].cName,
    bId: arr1[i].bId,
    bufName: arr1[i].bName,
    impFlag: arr2.filter(({cId}) => cId === arr1[i].cId).map(({flag}) => (flag)).toString()
  };
  rowData.push(obj);
}
console.log(rowData)

Hi, I'm checking if cId in arr2 is present in arr1 and based on it I'm updating the flag for which cId is present, but for the remaining values which are not present in arr2, impFlag is coming as "". It should remain the same as in arr1. I'm not sure how to add conditions accordingly??

Nick Parsons

Since "" is the only falsy value that you'll get from calling Array.prototype.toString(), you can use the || (OR) operator to provide a default value when there are no matching flags, eg:

arr2.filter(...).map(...).toString() || arr1[i].flag

Above, when .toString() results in a falsy "" value, the right-hand side of the || operator is used as the result instead. Note that .filter() is good if you have an array of results, which means that there are potentially multiple objects with the same cId in arr2. If you can only have one object, then .find() instead of filter would be more appropriate:

arr2.find(...)?.flag ?? arr1[i].flag

const rowData = [];
const arr1 = [{ "bId": "F1.1.4", "bName": "Mercedes", "cId": "150494", "cName": "Benz", "flag": "Maintain" }, { "bId": "F1.1.4", "bName": "Ford", "cId": "150744", "cName": "Mustang", "flag": "Maintain" }, { "bId": "F1.1.4", "bName": "Volkswagon", "cId": "4961", "cName": "Polo", "flag": "Maintain" }, { "bId": "F1.1.5", "bName": "Isdera", "cId": "N/A", "cName": "Isdera", "flag": "Test" } ];
const arr2 = [{ "cId": "150494", "flag": "Decommission" }, { "cId": "150744", "flag": "Invest" }, { "cId": "4961", "flag": "Pending" } ];


for (let i = 0; i < arr1.length; i++) {
  const obj = {
    custId: arr1[i].cId,
    custName: arr1[i].cName,
    bId: arr1[i].bId,
    bufName: arr1[i].bName,
    impFlag: arr2.filter(({cId}) => cId === arr1[i].cId).map(({flag}) => (flag)).toString() || arr1[i].flag
  };
  rowData.push(obj);
}
console.log(rowData)


Personally, I would write this logic as below. As you're performing a mapping operator, .map() is a good tool for this:

const arr1 = [{ "bId": "F1.1.4", "bName": "Mercedes", "cId": "150494", "cName": "Benz", "flag": "Maintain" }, { "bId": "F1.1.4", "bName": "Ford", "cId": "150744", "cName": "Mustang", "flag": "Maintain" }, { "bId": "F1.1.4", "bName": "Volkswagon", "cId": "4961", "cName": "Polo", "flag": "Maintain" }, { "bId": "F1.1.5", "bName": "Isdera", "cId": "N/A", "cName": "Isdera", "flag": "Test" } ];
const arr2 = [{ "cId": "150494", "flag": "Decommission" }, { "cId": "150744", "flag": "Invest" }, { "cId": "4961", "flag": "Pending" } ];


const cIdMap = new Map(arr2.map(({cId, flag}) => [cId, flag]));

const rowData = arr1.map((obj) => ({
  custId: obj.cId,
  custName: obj.cName,
  bId: obj.bId,
  bufName: obj.bName,
  impFlag: cIdMap.get(obj.cId) ?? obj.flag
}));

console.log(rowData)

Here, the Map creates a structure that maps the cId value to its corresponding flag value (this assumes each cId is unique in arr2). This Map is then accessed in the .map() callback, where we use the nullish coalescing operator ?? to make the current object's flag (obj.flag) the default if the Map doesn't contain a cId mapping for the current object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Map Array values from one Array to Another Array JavaScript?

From Dev

Atomically move object by ID from one array to another in same document

From Dev

Map one property from object array to another object in different object array

From Dev

how to map more than one property from array of object in javascript

From Dev

Map value of an attribute in one object array to an attribute in another object array Javascript

From Dev

Add all objects with the same id from one array into an array of an object with the same id in another array

From Dev

Subtract one object from another one with in array

From Dev

Map from one object to another in Ruby

From Dev

map array with more than one object into another array

From Dev

Move object from one array to another

From Dev

How to update one array with data from another array in Javascript using Key of Id?

From Dev

How to add one array object to the another array object in javascript

From Dev

Transform javascript object from one form to another

From Dev

Move a property from one object to another in Javascript

From Dev

Create object from another one Javascript

From Dev

How to pass json array object from one javascript adapter to another javascript adapter?

From Dev

How to subtract one object array literally from another object Array

From Dev

Moving array of object from one array to another array on button click

From Javascript

JavaScript get elements from an object array that are not in another

From Dev

add data from array of object to another in JavaScript

From Dev

Map New array of object from array of object javascript

From Dev

Replace object in array with data from another array's object in JavaScript

From Dev

Map an object from one image to another image using openCV and Python

From Dev

How to match array elements from one object into another object?

From Dev

Create one object from a array of objects in Javascript

From Dev

How to filter objects from one array from another array in JavaScript?

From Dev

Check if object from one array is included inside another array of objects

From Dev

How can I remove object in one array from another array?

From Dev

Typescript, map one object into another?

Related Related

  1. 1

    How to Map Array values from one Array to Another Array JavaScript?

  2. 2

    Atomically move object by ID from one array to another in same document

  3. 3

    Map one property from object array to another object in different object array

  4. 4

    how to map more than one property from array of object in javascript

  5. 5

    Map value of an attribute in one object array to an attribute in another object array Javascript

  6. 6

    Add all objects with the same id from one array into an array of an object with the same id in another array

  7. 7

    Subtract one object from another one with in array

  8. 8

    Map from one object to another in Ruby

  9. 9

    map array with more than one object into another array

  10. 10

    Move object from one array to another

  11. 11

    How to update one array with data from another array in Javascript using Key of Id?

  12. 12

    How to add one array object to the another array object in javascript

  13. 13

    Transform javascript object from one form to another

  14. 14

    Move a property from one object to another in Javascript

  15. 15

    Create object from another one Javascript

  16. 16

    How to pass json array object from one javascript adapter to another javascript adapter?

  17. 17

    How to subtract one object array literally from another object Array

  18. 18

    Moving array of object from one array to another array on button click

  19. 19

    JavaScript get elements from an object array that are not in another

  20. 20

    add data from array of object to another in JavaScript

  21. 21

    Map New array of object from array of object javascript

  22. 22

    Replace object in array with data from another array's object in JavaScript

  23. 23

    Map an object from one image to another image using openCV and Python

  24. 24

    How to match array elements from one object into another object?

  25. 25

    Create one object from a array of objects in Javascript

  26. 26

    How to filter objects from one array from another array in JavaScript?

  27. 27

    Check if object from one array is included inside another array of objects

  28. 28

    How can I remove object in one array from another array?

  29. 29

    Typescript, map one object into another?

HotTag

Archive