Home > Software design >  creating custom array by matching array n object in javascript / node js
creating custom array by matching array n object in javascript / node js

Time:02-17

let obj ={
  97:{
   data:"OS",
   location:"system",
   access:"globally",
   parameters:"aed31"
  },
  490:{
   data:"Ae-ds",
   location:"rootpath",
   access:"admin rights",
   parameters:"4fsje"
  },
  278:{
   data:"config",
   location:"system",
   access:"private",
   parameters:"aed31"
  },
  6230:{
   data:"secure-data",
   location:"system-private",
   access:"config-data",
   parameters:"aed31"
  },
  4023:{
   data:"linux",
   location:"system",
   access:"globally",
   parameters:"rf241"
  },
  870:{
   data:"deh-we",
   location:"adminpath",
   access:"admin rights",
   parameters:"4fsje"
  },
}

let data=[
 { name:"OS"
 },
 { name:"Ae-ds"
 },
 { name:"config"
 },
 { name:"secure-data"
 }
];

data.map((value)=>{
console.log(value.name)
})

I am trying to find and match the name which is present inside the data array with the obj.data field and want to store there key like this

let dataDetail= [{id:97, config_type:"system Data"}];

its like name OS or As-ds or any name which I got from the data.map() I want to check if it is available inside the object or not and fetch its key and push it in dataDetail array in id here config_type is constant I don't want to change that value only the id field get changed here

let data=[
 { name:"OS"
 },
 { name:"Ae-ds"
 },
 { name:"config"
 },
 { name:"secure-data"
 }
];

let jsonData={
  97:{
   data:"OS",
   location:"system",
   access:"globally",
   parameters:"aed31"
  },
  490:{
   data:"Ae-ds",
   location:"rootpath",
   access:"admin rights",
   parameters:"4fsje"
  },
  278:{
   data:"config",
   location:"system",
   access:"private",
   parameters:"aed31"
  },
  6230:{
   data:"secure-data",
   location:"system-private",
   access:"config-data",
   parameters:"aed31"
  },
  4023:{
   data:"linux",
   location:"system",
   access:"globally",
   parameters:"rf241"
  },
  870:{
   data:"deh-we",
   location:"adminpath",
   access:"admin rights",
   parameters:"4fsje"
  },
}
let details;
data.map((value)=>{

 Object.values(jsonData).map((dataName)=>{
  if(dataName.data===value.name){
   dataDetail=[{id:Object.keys(jsonData), config_type:"system Data"}];
   console.log("checking",dataDetail);
  }
 });
});

but its not working here and I don't know what missing here as my expected output is like If I console dataDetail;

[{id:97, config_type:"system Data"}]
[{id:490, config_type:"system Data"}]
[{id:278, config_type:"system Data"}]
[{id:6230, config_type:"system Data"}]

CodePudding user response:

Can be easily managed by a find inside a forEach

let data=[
 { name:"OS"
 },
 { name:"Ae-ds"
 },
 { name:"config"
 },
 { name:"secure-data"
 }
];

let jsonData={
  97:{
   data:"OS",
   location:"system",
   access:"globally",
   parameters:"aed31"
  },
  490:{
   data:"Ae-ds",
   location:"rootpath",
   access:"admin rights",
   parameters:"4fsje"
  },
  278:{
   data:"config",
   location:"system",
   access:"private",
   parameters:"aed31"
  },
  6230:{
   data:"secure-data",
   location:"system-private",
   access:"config-data",
   parameters:"aed31"
  },
}

let dataDetails = [];

Object.keys(jsonData).forEach((key) => {
    if(data.find((el) => el.name === jsonData[key].data)){
    dataDetails.push([{id:key, config_type:"system Data"}])
    console.log([{id:key, config_type:"system Data"}])
  }
})
console.log(dataDetails)

  • Related