Yathindra Kodithuwakku
Yathindra Kodithuwakku

Reputation: 135

JS Creating a custom json by a nested json

Here I need to convert my nested JSON into a custom JSON without having nested objects.

function transform(){  
    let items = [
        {
            "carId":328288,
            "firstName":"yathindra",
            "lastName":"rawya",
            "list":[
                {
                    "id":182396,
                    "isAvail":false,
                    "stateId":288,
                    "state":"Awesome"
                },
                {
                    "id":182396,
                    "isAvail":false,
                    "stateId":678,
                    "state":"Cool1"
                }
            ],
        },
        {
            "carId":3282488,
            "firstName":"yathindraR",
            "lastName":"K",
            "list":[
                {
                    "id":18232396,
                    "isAvail":false,
                    "stateId":22388,
                    "state":"Awesome"
                },
                {
                    "id":182356796,
                    "isAvail":false,
                    "stateId":45678,
                    "state":"Cool"
                }
            ],
        }
    ]

    let customList = [];

    for(let i=0;i<items.length;i++){

        let temp = new Array()
        for(let j=0;j<items[i].list.length;j++){
            temp.push(
                items[i].list[j].state
            )
        }
        
        customList.push({
            fname: items[i].firstName,
            lname: items[i].lastName,
            ...temp
        })
    }

    console.log(JSON.stringify(customList))
}

transform();

Below is the output I am getting.

[{"0":"Awesome","1":"Cool1","fname":"yathindra","lname":"rawya"},{"0":"Awesome","1":"Cool","fname":"yathindraR","lname":"K"}]

But I don't want to place items in the temp array in the beginning. I want them to place at last. Something like this.

[{"fname":"yathindra","lname":"rawya","0":"Awesome","1":"Cool1"},{"fname":"yathindraR","lname":"K","0":"Awesome","1":"Cool"}]

Here there is no need of using numbers as keys in the temp array. Because I want only values of each. So its ok if the keys of all are strings and order of values matters. How to make this done?

Upvotes: 2

Views: 138

Answers (3)

Steven Spungin
Steven Spungin

Reputation: 29071

Prepend your key with a zero, and then insertion order is maintained. In a CSV, that seems a good option.

If you want to use the spread operator, use an object instead of an array.

function transform() {
  let items = [{
      "carId": 328288,
      "firstName": "yathindra",
      "lastName": "rawya",
      "list": [{
          "id": 182396,
          "isAvail": false,
          "stateId": 288,
          "state": "Awesome"
        },
        {
          "id": 182396,
          "isAvail": false,
          "stateId": 678,
          "state": "Cool1"
        }
      ],
    },
    {
      "carId": 3282488,
      "firstName": "yathindraR",
      "lastName": "K",
      "list": [{
          "id": 18232396,
          "isAvail": false,
          "stateId": 22388,
          "state": "Awesome"
        },
        {
          "id": 182356796,
          "isAvail": false,
          "stateId": 45678,
          "state": "Cool"
        }
      ],
    }
  ]

  let customList = [];

  for (let i = 0; i < items.length; i++) {

    let temp = {}
    for (let j = 0; j < items[i].list.length; j++) {
      temp['0' + j] = items[i].list[j].state
    }

    const o = {
      fname: items[i].firstName,
      lname: items[i].lastName,
      ...temp
    }

    customList.push(o)
  }

  console.log(JSON.stringify(customList))
}

transform();

That said, there are other ways you can map your items...

function transform() {
  let items = [{
      "carId": 328288,
      "firstName": "yathindra",
      "lastName": "rawya",
      "list": [{
          "id": 182396,
          "isAvail": false,
          "stateId": 288,
          "state": "Awesome"
        },
        {
          "id": 182396,
          "isAvail": false,
          "stateId": 678,
          "state": "Cool1"
        }
      ],
    },
    {
      "carId": 3282488,
      "firstName": "yathindraR",
      "lastName": "K",
      "list": [{
          "id": 18232396,
          "isAvail": false,
          "stateId": 22388,
          "state": "Awesome"
        },
        {
          "id": 182356796,
          "isAvail": false,
          "stateId": 45678,
          "state": "Cool"
        }
      ],
    }
  ]

  let customList = items.map(item => {
    let j = 0;
    let list = item.list.reduce((acc, it) => {
      acc['0' + j++] = it.state;
      return acc
    }, {})

    return {
      fname: item.firstName,
      lname: item.lastName,
      ...list
    }
  })
  console.log(JSON.stringify(customList))
}

transform();

Upvotes: 0

Mohsen ZareZardeyni
Mohsen ZareZardeyni

Reputation: 946

Javascript Object first shows the sorted number list and then, the rest of the object's content as it is! run the following code and see what happens:D

console.log(JSON.stringify({1:true, b:false, 3:false}))

So if you want to keep your order, don't use numbers as keys!

Upvotes: 0

Bhavin Poojara
Bhavin Poojara

Reputation: 71

Please replace your code with below one, it will work straight away. Key will be "state-1", "state-2" instead of "0", "1"

function transform(){  
    let items = [
        {
            "carId":328288,
            "firstName":"yathindra",
            "lastName":"rawya",
            "list":[
                {
                    "id":182396,
                    "isAvail":false,
                    "stateId":288,
                    "state":"Awesome"
                },
                {
                    "id":182396,
                    "isAvail":false,
                    "stateId":678,
                    "state":"Cool1"
                }
            ],
        },
        {
            "carId":3282488,
            "firstName":"yathindraR",
            "lastName":"K",
            "list":[
                {
                    "id":18232396,
                    "isAvail":false,
                    "stateId":22388,
                    "state":"Awesome"
                },
                {
                    "id":182356796,
                    "isAvail":false,
                    "stateId":45678,
                    "state":"Cool"
                }
            ],
        }
    ]

    let customList = [];

    for(let i=0;i<items.length;i++){

        let temp = {};
        for(let j=0;j<items[i].list.length;j++){
            temp["state-"+(j+1)] = items[i].list[j].state;
        }

        customList.push({
            fname: items[i].firstName,
            lname: items[i].lastName,
            ...temp
        })
    }

    console.log(JSON.stringify(customList))
}

transform();

Upvotes: 1

Related Questions