Match array values and create final array - Javascript

Viewed 73

I have below array. First object is the original data. Inside array is the changed value. I am trying to create a final data by matching with the Name field with inside array. which should look like

var a = 
[
  {"Id":"1","Test":"Name1","Name":"hunt9988ggggggggggggdfsf1111"},
  {"Id":"2","Test":"Name2","Name":"hunt9988ggggggggggggdfsf"},
  [
    **{"Name":"hunt9988ggggggggggggdfsf1118","Id":"1"},   // Changed value
    {"Name":"hunt9988ggggggggggggdfsf1118","Id":"2"}**
  ]
]

Final Data

var a = 
[
  {"Id":"1","Test":"Name1","Name":"hunt9988ggggggggggggdfsf1118"},
  {"Id":"2","Test":"Name2","Name":"hunt9988ggggggggggggdfsf1118"}
]

I am trying with below code

var result = a.map(item => ({ value: item.Id, text: item.Name}));
console.log(result)
1 Answers

Like this?

Note I modify the original array

let a = [{
    "Id": "1",
    "Test": "Name1",
    "Name": "hunt9988ggggggggggggdfsf1111"
  },
  {
    "Id": "2",
    "Test": "Name2",
    "Name": "hunt9988ggggggggggggdfsf"
  },
  [{
      "Name": "hunt9988ggggggggggggdfsf1118",
      "Id": "1"
    }, // Changed value
    {
      "Name": "hunt9988ggggggggggggdfsf1118",
      "Id": "2"
    }
  ]
]
const replaceArray = a.find(item => Array.isArray(item))
replaceArray.forEach(item => a.find(aItem => aItem.Id === item.Id).Name=item.Name)
a = a.filter(item => item.Id)
console.log(a)

Related