5

I am trying to de-structure an array of object and create sub objects from it I have an array an object like

const data = [
        {
            "id": 1,
            "locationone": "California, United States",
            "idtwo": 2,
            "locationtwo": "Atherton, United States"
        },
        {
            "id": 3,
            "locationone": "London",
            "idtwo": 4,
            "locationtwo": "New Jersey"
        }
    ]

I am trying to achieve the following result

[
  {
    id : 1,
    location : "California, United States"
  },
{
    id : 2,
    location : "Atherton, United States"
  },
{
    id : 3,
    location : "London"
  },
{
    id : 4,
    location : "New Jersey"
  },
]

I tried the following approach but it didn't work

const result= data
        .map(({ id,locationone, idtwo, locationtwo }) => ({
          name: locationone,
          id: id,
            name: locationtwo,
            id : idtwo
        
        }))

Also is there a way that newly created result array has only elements with unique id?

6 Answers 6

7

You can simply map the array to get a new array for each item and then flattern the array.

Note : This can be done using directly flatMap

Example:

const array = [{
    "id": 1,
    "locationone": "California, United States",
    "idtwo": 2,
    "locationtwo": "Atherton, United States"
  },
  {
    "id": 3,
    "locationone": "London",
    "idtwo": 4,
    "locationtwo": "New Jersey"
  }
]

const newArray = array.flatMap(({
  id,
  locationone,
  idtwo,
  locationtwo
}) => {
  return [{
      id,
      location: locationone
    },
    {
      id: idtwo,
      location: locationtwo
    }
  ]
})

console.log(newArray)

Sign up to request clarification or add additional context in comments.

2 Comments

can we also filter out the object with unique id in the same go?
If you use .flatMap instead of map directly, you don't have to flat at the end
4

One option is to use .flatMap() by mapping each of your objects to an array that contains two objects (one with loccationone and another with locationtwo). The returned array of objects are flattened into the one resulting array:

const data = [ { "id": 1, "locationone": "California, United States", "idtwo": 2, "locationtwo": "Atherton, United States" }, { "id": 3, "locationone": "London", "idtwo": 4, "locationtwo": "New Jersey" } ];

const result = data.flatMap(obj => [
  {name: obj.locationone, id: obj.id},
  {name: obj.locationtwo, id : obj.idtwo}
]);
console.log(result);

3 Comments

can we also filter out the object with unique id in the same go?
@durayfishan if flatMap() returns an array then you can do whatever you can in normal array after flatMap() like flatMap().filter() and store the final result
@durayfishan what do you mean by filter out object with unique id? If there are two objects with the same id, then do you want to keep the first and remove the second, or keep the second and remove the first? Or remove both...?
1

Loop over the data, destructure the various properties from each object, and then push new two objects using those properties into a new array.

const data=[{id:1,locationone:"California, United States",idtwo:2,locationtwo:"Atherton, United States"},{id:3,locationone:"London",idtwo:4,locationtwo:"New Jersey"}];

const out = [];

for (const obj of data) {
  const { id, locationone, idtwo, locationtwo } = obj;
  out.push({ id, location: locationone });
  out.push({ id: idtwo, location: locationtwo });
}

console.log(out);

Comments

1
let newData = new Array(0)
data.forEach(element=> {
    newData.push({id: element.id, location: element.locationone})
    newData.push({id: element.idtwo, location: element.locationtwo})
})
console.log(newData)

2 Comments

map returns an array. If you're going this route you should use forEach.
Feel free to edit your answer then.
0

A solution using reduce()

const data = [{
    "id": 1,
    "locationone": "California, United States",
    "idtwo": 2,
    "locationtwo": "Atherton, United States"
  },
  {
    "id": 3,
    "locationone": "London",
    "idtwo": 4,
    "locationtwo": "New Jersey"
  }
]

let result = data.reduce((a, v) => {
  let obj1 = {}
  obj1.id = v.id
  obj1.location = v.locationone
  a.push(obj1)
  let obj2 = {}
  obj2.id = v.idtwo
  obj2.location = v.locationtwo
  a.push(obj2)
  return a
}, [])

console.log(result)

Comments

0

You can achieve the desired solution on multiple ways, for example let's use the high order function reduce instead on map.

const data = [
        {
            "id": 1,
            "locationone": "California, United States",
            "idtwo": 2,
            "locationtwo": "Atherton, United States"
        },
        {
            "id": 3,
            "locationone": "London",
            "idtwo": 4,
            "locationtwo": "New Jersey"
        }
    ]


const result = data.reduce((prev, {id, idtwo, locationone, locationtwo}) => {
  const newObjOne = {
    "id": id,
    "location": locationone
  }

  const newObjTwo = {
    "id": idtwo,
    "location": locationtwo
  }
  return [
    ...prev,
    newObjOne,
    newObjTwo
  ]
}, [])

console.log(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.