Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list of objects to a nested list of Objects javascript

I am new to JS. I have a list of Objects which looks like below,

var a = [
  {
    wa_id: 1,
    wa_property_id: 'p1',
    wa_view_name: 'ram'
  },
  {
    wa_id: 1,
    wa_property_id: 'p1',
    wa_view_name: 'sam'
  },
  {
    wa_id: 1,
    wa_property_id: 'p2',
    wa_view_name: 'kam'
  },
  {
    wa_id: 2,
    wa_property_id: 'p5',
    wa_view_name: 'pri'
  },
   {
    wa_id: 1,
    wa_property_id: 'p3',
    wa_view_name: 'ste',
  },
]

I want to iterate over these objects and create a new separate object. I want the required Output like below

Required Output:

  {
        "1": {
            "p1": ["ram", "sam"],
            "p2": ["kam"],
            "p3": ["ste"]
        },
        "2": {
            "p5": ["pri"]
        }
    }

Here's the Code I tried,

var a = [{
    wa_id: 1,
    wa_property_id: 'p1',
    wa_view_name: 'ram'
  },
  {
    wa_id: 1,
    wa_property_id: 'p1',
    wa_view_name: 'sam'
  },
  {
    wa_id: 1,
    wa_property_id: 'p2',
    wa_view_name: 'kam'
  },
  {
    wa_id: 2,
    wa_property_id: 'p5',
    wa_view_name: 'pri'
  },
  {
    wa_id: 1,
    wa_property_id: 'p3',
    wa_view_name: 'ste',
  },

]
temp_obj = {}
temp_list = []
for (var j = 0; j < a.length; j++) {
  temp_list.push(a[j].wa_property_id)
}
temp_list.splice(0, temp_list.length, ...(new Set(temp_list)))
// Property ID List
console.log(temp_list)
// Output: ["p1","p2","p5","p3"]

properyViewObj = {}
temp_list2 = []
for (var j = 0; j < temp_list.length; j++) {
  for (var k = 0; k < a.length; k++) {
    if (a[k].wa_property_id == temp_list[j]) {
      temp_list2.push(a[k].wa_view_name)
    }
  }
  properyViewObj[temp_list[j]] = temp_list2
  temp_list2 = []
}
// Property with Viewnames
console.log(properyViewObj)
// Output: {p1: ["ram","sam"],p2: ["kam"],p5: ["pri"],p3: ["ste"]}

var wa_list = []
for (var j = 0; j < a.length; j++) {
  wa_list.push(a[j].wa_id)
}
wa_list.splice(0, wa_list.length, ...(new Set(wa_list)))
// Wa_id List
console.log(wa_list)
// Output: [1,2]
// How to make the Next Step
var output = {}
var tem = []
for (var j = 0; j < wa_list.length; j++) {
  for (var k = 0; k < a.length; k++) {
    if (a[k].wa_id == wa_list[j]) {
      console.log(a[k].wa_property_id)

    }
  }
}
like image 469
Stack Kiddy Avatar asked Apr 02 '20 11:04

Stack Kiddy


People also ask

How to convert an object to an array in JavaScript?

Also read, how to convert an Object to an Array in Javascript. 2. Using Spread Operator In this above code snippet, we have used the reduce function and along with that, we have used spread operator (…) which will basically spread the previous object for every iteration and add new values to it.

How to access nested array in JavaScript?

Array reduce method is very powerful and it can be used to safely access nested objects. // to access nested array, just pass in array index as an element the path array.

How do I get the city of a nested array?

// to access nested array, just pass in array index as an element the path array. // this will return the city from the first address item. If you think the above methods are a lil’ too mainstream, then you should try Typy library that I’ve written.


2 Answers

You can use a combination of reduce and spread to compute your object.

const data = a.reduce((lkp, obj, i) => {
  const {wa_id, wa_property_id, wa_view_name} = obj;
  return {
    ...lkp,
    [wa_id]: {
      ...lkp[wa_id],
      [wa_property_id]: [...((lkp[wa_id] || {})[wa_property_id] || []), wa_view_name]
    }
  }
}, {})

console.log (data)
<script>
  var a = [{
      wa_id: 1,
      wa_property_id: 'p1',
      wa_view_name: 'ram'
    },
    {
      wa_id: 1,
      wa_property_id: 'p1',
      wa_view_name: 'sam'
    },
    {
      wa_id: 1,
      wa_property_id: 'p2',
      wa_view_name: 'kam'
    },
    {
      wa_id: 2,
      wa_property_id: 'p5',
      wa_view_name: 'pri'
    },
    {
      wa_id: 1,
      wa_property_id: 'p3',
      wa_view_name: 'ste',
    },
  ]
</script>
like image 67
Moritz Roessler Avatar answered Sep 21 '22 15:09

Moritz Roessler


YOu can try reduce your array to object

var a = [
  {
    wa_id: 1,
    wa_property_id: 'p1',
    wa_view_name: 'ram'
  },
  {
    wa_id: 1,
    wa_property_id: 'p1',
    wa_view_name: 'sam'
  },
  {
    wa_id: 1,
    wa_property_id: 'p2',
    wa_view_name: 'kam'
  },
  {
    wa_id: 2,
    wa_property_id: 'p5',
    wa_view_name: 'pri'
  },
   {
    wa_id: 1,
    wa_property_id: 'p3',
    wa_view_name: 'ste',
  },
]

let result = a.reduce((acc,rec) =>{

//if result object doesn't contain key for wa_id - add new id key
if(!(Object.keys(acc).includes(rec.wa_id.toString())))
{
  return {...acc, [rec.wa_id]: {[rec.wa_property_id]:[rec.wa_view_name]}}
}

//if result id object doesn't contain key for property - add new property key
if(!(Object.keys(acc[rec.wa_id]).includes(rec.wa_property_id.toString())))
{
    acc[rec.wa_id] = {...acc[rec.wa_id],[rec.wa_property_id]:[rec.wa_view_name] }
  return acc
}

//otherwise add new value to array of properties
acc[rec.wa_id][rec.wa_property_id] = [...acc[rec.wa_id][rec.wa_property_id], rec.wa_view_name]
return acc
},{})

console.log(result)
like image 34
elvira.genkel Avatar answered Sep 23 '22 15:09

elvira.genkel