Home > database >  How to index an array with map method consisting of objects and arrays
How to index an array with map method consisting of objects and arrays

Time:07-08

I have an array that is made from another array with the map method in JavaScript:

response = initialResponse.data.Resurs.map((item)=>({
    KomRes:item.Kom,
    levels:
    [ 
    ...item.NumList.map((item)=>(
      {
      KomRes:item.Number,
      })),
    ...item.SerList.map((item,index3)=>({
      KomRes:"Serial: "   item.Ser,
      })),
  ]}));

So, I have an array of 1 object and one array of objects. Now, I want to add indexes so that the parent object and all of its child objects have different indexes. One example would be:

[
{
 KomRes:"abc"
 id:1 // ==> Here the id is different to the levels objects id-s
 levels:[{KomRes:"cde",id:2},{KomRes:"cdef",id:3}]
},
{
 KomRes:"dfr"
 id:4 // ==> Here the id is different to the levels objects id-s
 levels:[{KomRes:"dsf",id:5},{KomRes:"sgsd",id:6}]
},
{
 KomRes:"fgr"
 id:7 // ==> Here the id is different to the levels objects id-s
 levels:[{KomRes:"zizu",id:8},{KomRes:"hkl",id:9}]
},
]

As you can see, all of the objects have different ids (indexes). How can I achieve that?

I tried to add index to map method, but don't know how to achieve that with child map methods:

response = initialResponse.data.Resurs.map((item,index)=>({
    KomRes:item.Kom,
    id:index,
    levels:
    [ 
    ...item.NumList.map((item)=>(
      {
      KomRes:item.Number,
      })),
    ...item.SerList.map((item,index3)=>({
      KomRes:"Serial: "   item.Ser,
      })),
  ]}));

CodePudding user response:

Define a counter variable outside the function then on each iteration each object is given id property with an incremented value of the counter variable. Should there be any sub-arrays, they will be handled recursively by calling itself and passing in the sub-array.

const data=[{KomRes:"abc",id:null,levels:[{KomRes:"cde",id:null},{KomRes:"cdef",id:null}]},{KomRes:"ghi",id:null,levels:[{KomRes:"ijk",id:null},{KomRes:"ijkl",id:null}]},{KomRes:"mno",id:null,levels:[{KomRes:"omn",id:null},{KomRes:"omnp",id:null}]}];

let idx = 1;

function flatIndex(array) {
  return array.map(obj => {
    if (!obj.id) {
      obj.id = idx  ;
    }
    Object.values(obj).map(v => {
      if (Array.isArray(v)) {
        return flatIndex(v);
      }
      return obj;
    });
    return obj;
  });
}

console.log(flatIndex(data));

CodePudding user response:

Not sure if I understand well what you want to achieve, but you can declare a variable out of the scope and increment it along. This gives the result you expect

const response = [
  { Kom: 'abc', NumList: [{ Number: "cde"}], SerList: [{ Ser: "cdef" }] },
  { Kom: 'dfr', NumList: [{ Number: "dsf"}], SerList: [{ Ser: "sgsd"}] },
  { Kom: 'fgr', NumList: [{ Number: "zizu"}], SerList: [{ Ser: "hkl"}] }
];

let lastId = 1; // index var to increment
const result = response.map((item) => ({
    KomRes: item.Kom,
    id: lastId  ,
    levels: [ 
      ...item.NumList.map((item) => ({
        id: lastId  ,
        KomRes: item.Number,
      })
     ),
     ...item.SerList.map((item) => ({
        id: lastId  ,
        KomRes: "Serial: "   item.Ser,
      })
     ),
   ]
  })
 );
  
  console.log(result)

  • Related