Home > Software design >  Convert a const Array to JSON with specific keys
Convert a const Array to JSON with specific keys

Time:12-10

I have an array like this:

const faces= [
    [[128516], "grinning face with smiling eyes", "20201001"],
    [[128512], "grinning face", "20201001"],
    [[128578], "slightly smiling face", "20201001"],
    [[128579], "upside-down face", "20201001"],
    [[128521], "winking face", "20201001"]
]

And I want to convert it to a formatted JSON like this using Java or JavaScript, different but both are fine:

[
  {
    "id": 128516,
    "name": "grinning face with smiling eyes",
    "date": "20201001"
  },
  {
    "id": 128512,
    "name": "grinning face",
    "date": "20201001"
  }
]

Any help is appreciated.

CodePudding user response:

const faces = [
  [
    [128516], "grinning face with smiling eyes", "20201001"
  ],
  [
    [128512], "grinning face", "20201001"
  ],
  [
    [128578], "slightly smiling face", "20201001"
  ],
  [
    [128579], "upside-down face", "20201001"
  ],
  [
    [128521], "winking face", "20201001"
  ]
]

let result = []
faces.forEach(item => {
  let a = {
    id: item[0][0],
    name: item[1],
    date: item[2]
  }
  result.push(a)
})

console.log(JSON.stringify(result))

CodePudding user response:

You could use reduce:

const faces = [
  [
    [128516], "grinning face with smiling eyes", "20201001"
  ],
  [
    [128512], "grinning face", "20201001"
  ],
  [
    [128578], "slightly smiling face", "20201001"
  ],
  [
    [128579], "upside-down face", "20201001"
  ],
  [
    [128521], "winking face", "20201001"
  ]
]

const result = faces.reduce((result, value) => {
  result.push({
    id: value[0][0],
    name: value[1],
    date: value[2]
  })
  return result
}, [])

console.log(result)

CodePudding user response:

Simple one liner code using 'map' functionality of Arrays.

const faces= [
    [[128516], "grinning face with smiling eyes", "20201001"],
    [[128512], "grinning face", "20201001"],
    [[128578], "slightly smiling face", "20201001"],
    [[128579], "upside-down face", "20201001"],
    [[128521], "winking face", "20201001"]
];

let newObj = []; 

faces.map(eachObj => {
  const obj = {
   id: eachObj[0][0],
   name: eachObj[1],
   date: eachObj[2]
  };
  newObj.push(obj);
});

console.log('New Obj ==>', newObj);

CodePudding user response:

You can do it like this:

const faces= [
  [[128516], "grinning face with smiling eyes", "20201001"],
  [[128512], "grinning face", "20201001"],
  [[128578], "slightly smiling face", "20201001"],
  [[128579], "upside-down face", "20201001"],
  [[128521], "winking face", "20201001"]
]
const formatedFaces = faces.reduce((acc, item)=> {
  const obj ={
    id: item[0][0],
    name: item[1],
    date: item[2]
  };
  acc.push(obj);
  return acc;
}, []);

console.log(JSON.stringify(formatedFaces))

CodePudding user response:

You can map

const faces= [
    [[128516], "grinning face with smiling eyes", "20201001"],
    [[128512], "grinning face", "20201001"],
    [[128578], "slightly smiling face", "20201001"],
    [[128579], "upside-down face", "20201001"],
    [[128521], "winking face", "20201001"]
]

let result = faces.map(([ [id], name, date ]) => ({ id, name, date }))

console.log(result)

  • Related