Home > Enterprise >  arrange data in some format
arrange data in some format

Time:08-20

I am new to javascript. I need to arrange the data, in some particular format.

[{id:1,isNew:"no"},
{id:2,isNew:"no"},
{id:3,isNew:"yes"},
{id:4,isNew:"no"},
{id:5,isNew:"no"},
{id:6,isNew:"no"},
{id:7,isNew:"yes"},
{id:8,isNew:"no"},
{id:9,isNew:"no"},
{id:10,isNew:"yes"}]

I need to manipulate data as this -

   [{
     id:1,
     isNew:"no"
     },
    {
      id:2,
      isNew:"no"
    },
    {
      id:3,
      isNew:"yes", 
      "sub":[ 
             {id:4,isNew:"no"},
             {id:5,isNew:"no"},
             {id:6,isNew:"no"}
            ]
     },
    {
      id:7,
      isNew:"yes",
      "sub":[
             {id:8,isNew:"no"},
             {id:9,isNew:"no"}
           ]
     },
    {
     id:10,
     isNew:"yes"
    }
]

In short, if i get a yes, i need to show next results under it, till the time another "yes" comes up and so on.

I tired, first taking out the indexes of all yes, and if index of those elements lies between the range of yes array, set is inside another array.

CodePudding user response:

Seems a problem that can be solved with a simple loop and an aux array.

const myList = [
    {id:1,isNew:"no"},
    {id:2,isNew:"no"},
    {id:3,isNew:"yes"},
    {id:4,isNew:"no"},
    {id:5,isNew:"no"},
    {id:6,isNew:"no"},
    {id:7,isNew:"yes"},
    {id:8,isNew:"no"},
    {id:9,isNew:"no"},
    {id:10,isNew:"yes"}
];

const newList = [];

for (const x of myList) {
    if (x.isNew == 'yes' || !newList.some(({ isNew }) => isNew == 'yes')) newList.push(x);
    else if (newList[newList.length - 1].sub) newList[newList.length - 1].sub.push(x);
    else newList[newList.length - 1].sub = [x];
}

console.log(newList);

CodePudding user response:

This should work as you expected.

const allData = [{id:1,isNew:"no"},
{id:2,isNew:"no"},
{id:3,isNew:"yes"},
{id:4,isNew:"no"},
{id:5,isNew:"no"},
{id:6,isNew:"no"},
{id:7,isNew:"yes"},
{id:8,isNew:"no"},
{id:9,isNew:"no"},
{id:10,isNew:"yes"}]

let format = []
let yesData = null // used to store yes data
for(const data of allData) {
    const currentIterationHasYes = data.isNew === "yes"
    
    if(currentIterationHasYes) {
      yesData && format.push(yesData)
      yesData = data
      continue;
    }
   // newData is not present and is of no value
    if(!yesData) {
      format.push(data);
      continue;
    }
    // we have newData and do not have sub array
  if(!yesData['sub']) {
      yesData['sub'] = []
  }
    yesData['sub'].push(data)
}
format.push(yesData)
console.log(format)

CodePudding user response:

Try this

var data = [
  {
    id: 1,
    isNew: "no",
  },
  {
    id: 2,
    isNew: "no",
  },
  {
    id: 3,
    isNew: "yes",
  },
  {
    id: 4,
    isNew: "no",
  },
  {
    id: 5,
    isNew: "no",
  },
  {
    id: 6,
    isNew: "no",
  },
  {
    id: 7,
    isNew: "yes",
  },
  {
    id: 8,
    isNew: "no",
  },
  {
    id: 9,
    isNew: "no",
  },
  {
    id: 10,
    isNew: "yes",
  },
];

let finalData = [],
  mainIndex = -1;
for (let index = 0; index < data.length; index  ) {
  const current = data[index];
  const next = (index   1 < data.length && data[index]) || undefined;

  if (current.isNew === "yes") {
    finalData.push(current);
    mainIndex = finalData.findIndex((el) => el.isNew === "yes" && el.id === current.id);
  } 
  
  if (mainIndex >= 0 && next && next.isNew === "no") {
    if (Array.isArray(finalData[mainIndex].sub)) {
      finalData[mainIndex].sub.push(next);
    } else {
      finalData[mainIndex].sub = [next];
    }
  } 
   
  if (mainIndex < 0 && current.isNew === "no") {
    finalData.push(current);
  }
  
}


console.log("finalData :>> ", JSON.stringify(finalData, null, 2));

CodePudding user response:

Add new property to each object of your array to find parent of each item in array. and do following.

var my_arr = [{id:1,isNew:"no", "parent": null},
{id:2,isNew:"no", "parent": null},
{id:3,isNew:"yes", "parent": null},
{id:4,isNew:"no", "parent": 3},
{id:5,isNew:"no", "parent": 3},
{id:6,isNew:"no", "parent": 3},
{id:7,isNew:"yes", "parent": null},
{id:8,isNew:"no", "parent": 7},
{id:9,isNew:"no", "parent": 7},
{id:10,isNew:"yes", "parent": null}];


my_arr.forEach(function(item, index){
  if(item.parent) {
    my_arr[item.parent].sub = [item];
  }
});
my_arr = my_arr.filter(x => x.parent === null);

my_arr will be what you want.

  • Related