动态迭代json数组并添加到另一个数组

问题描述 投票:0回答:3

我有一大堆json对象,我需要迭代并将它们添加到另一个json数组。

该阵列如下所示 -

{
  id : 1,
  name : "test1",
  location : "London",
  cost: "120.83"
},
{
  id : 2,
  name : "test2",
  location : "shanghai",
  cost: "124.83"
},
{
  id : 3,
  name : "test3",
  location : "Barcelona",
  cost: "56.54"
}

我使用的实际数组有73个键,值对和大约20000个对象。

我试图将每个单独的对象转换为另一个数组,需要看起来像 -

{
  cells: 
    [
       {
         value: 1,
         textAlign: "center",
         background: "rgb(255,255,255)",
         color: "rgb(0,62,117)"
       },
       {
         value: "test1",
         textAlign: "center",
         background: "rgb(255,255,255)",
         color: "rgb(0,62,117)"
       },
       {
         value: "London",
         textAlign: "center",
         background: "rgb(255,255,255)",
         color: "rgb(0,62,117)"
       },
       {
         value: "120.83",
         textAlign: "center",
         background: "rgb(255,255,255)",
         color: "rgb(0,62,117)"
       },
    ]
},
{
  cells: 
    [
       {
          value: 2,
          textAlign: "center",
          background: "rgb(255,255,255)",
          color: "rgb(0,62,117)"
       },
       {
          value: "test2",
          textAlign: "center",
          background: "rgb(255,255,255)",
          color: "rgb(0,62,117)"
       },
       {
          value: "shanghai",
          textAlign: "center",
          background: "rgb(255,255,255)",
          color: "rgb(0,62,117)"
       },
       {
          value: "124.83",
          textAlign: "center",
          background: "rgb(255,255,255)",
          color: "rgb(0,62,117)"
       },
   ]
},

我只是无法绕过它,它必定是星期一的雾!

谢谢

javascript arrays json
3个回答
1
投票

你可以用Array.map()Object.values()来做到这一点:

const data = [
  { id : 1, name : "test1", location : "London", cost: "120.83" },
  { id : 2, name : "test2", location : "shanghai", cost: "124.83" },
  { id : 3, name : "test3", location : "Barcelona", cost: "56.54" }
];

const cell = { textAlign: 'center', background: 'rgb(255,255,255)', color: 'rgb(0,62,117)' };

const result = data.map(x =>
  ({ cells: Object.values(x).map(v => ({ value: `${v}`, ...cell })) })
);

console.log(result);

0
投票

你可以使用.map()Object.assign()方法:

const data = [
  {id : 1, name : "test1", location : "London", cost: "120.83"},
  {id : 2, name : "test2", location : "shanghai", cost: "124.83"},
  {id : 3, name : "test3", location : "Barcelona", cost: "56.54"}
];

const obj = {
  textAlign: "center",
  background: "rgb(255,255,255)",
  color: "rgb(0,62,117)"
};

const result = data.map(o => ({
   cells: Object.keys(o).map(k => Object.assign({}, {value: o[k]}, obj))
}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0
投票

你可以使用mapreduce

let arr = [{id:1,name:"test1",location:"London",cost:"120.83"},{id:2,name:"test2",location:"shanghai",cost:"124.83"},{id:3,name:"test3",location:"Barcelona",cost:"56.54"}]

let defaultObject = {textAlign: "center",background: "rgb(255,255,255)",color: "rgb(0,62,117)" }

let op = arr.map(e => Object.values(e).reduce((op,inp) => {
    op.cells.push({value: inp, ...defaultObject } )
    return op
  },{cells:[]})
)

console.log(op)
© www.soinside.com 2019 - 2024. All rights reserved.