javascript 减少嵌套对象的数组

7cwmlq89  于 2023-05-16  发布在  Java
关注(0)|答案(4)|浏览(153)

我有一个嵌套对象的动态数组如下:

[
    {
        "fields": [
            {
                "field-1": {
                    "id": "field-1",
                    "value": "a1"
                },
                "field-2": {
                    "id": "field-2",
                    "value": "a2"
                },
                "field-3": {
                    "id": "field-3",
                    "value": "a3"
                }
            }
        ]
    },
    {
        "fields": [
            {
                "field-1": {
                    "id": "field-1",
                    "value": "b1"
                },
                "field-2": {
                    "id": "field-2",
                    "value": "b2"
                },
                "field-3": {
                    "id": "field-3",
                    "value": "b3"
                }
            }
        ]
    }
]

我想解析/减少这个数组到下面的结构:

[
    {
        "field-1": "a1",
        "field-2": "a2",
        "field-3": "a3"
    },
    {
        "field-1": "b1",
        "field-2": "b2",
        "field-3": "b3"
    },
]

虽然我已经有了一种遍历数组并构造新数组的详细方法,但我想知道是否可以使用reduce方法获得结构,因为这样会更快?

fcg9iug3

fcg9iug31#

你可以用reduce来做

const result = raw
  .map(row => Object.entries(row.fields[0])
    .reduce((acc, [key, { value }]) => {
       acc[key] = value
       return acc
    }, {})
  )

console.log(result)
<script>
  const raw = [{
      "fields": [{
        "field-1": {
          "id": "field-1",
          "value": "a1"
        },
        "field-2": {
          "id": "field-2",
          "value": "a2"
        },
        "field-3": {
          "id": "field-3",
          "value": "a3"
        }
      }]
    },
    {
      "fields": [{
        "field-1": {
          "id": "field-1",
          "value": "b1"
        },
        "field-2": {
          "id": "field-2",
          "value": "b2"
        },
        "field-3": {
          "id": "field-3",
          "value": "b3"
        }
      }]
    }
  ]
</script>
yh2wf1be

yh2wf1be2#

JSON.parse reviver和JSON.stringify replacer可用于修改嵌套值:

const data = [{"fields":[{"field-1":{"id":"field-1","value":"a1"},
                          "field-2":{"id":"field-2","value":"a2"},
                          "field-3":{"id":"field-3","value":"a3"}}]},
              {"fields":[{"field-1":{"id":"field-1","value":"b1"},
                          "field-2":{"id":"field-2","value":"b2"},
                          "field-3":{"id":"field-3","value":"b3"}}]}]

let result = JSON.parse(JSON.stringify(data), (k, v) => v.value ?? v.fields?.[0] ?? v)

console.log( result )
fwzugrvs

fwzugrvs3#

你可以试试这个代码

var convert=[];
for (let i=0; i< test.length; i++){
    var obj={};
    Object.entries(test[i]).forEach(entry => { 
        const [key, value] = entry; 
        for(let j=0; j < value.length; j++){
            Object.entries(value[j]).forEach(s => { var [ k,v ] = s; obj[k]= v.value  }); 
        }
    })
    convert[i]=obj
}
console.log(convert);
<script>
var test=[
    {
        "fields": [
            {
                "field-1": {
                    "id": "field-1",
                    "value": "a1"
                },
                "field-2": {
                    "id": "field-2",
                    "value": "a2"
                },
                "field-3": {
                    "id": "field-3",
                    "value": "a3"
                }
            }
        ]
    },
    {
        "fields": [
            {
                "field-1": {
                    "id": "field-1",
                    "value": "b1"
                },
                "field-2": {
                    "id": "field-2",
                    "value": "b2"
                },
                "field-3": {
                    "id": "field-3",
                    "value": "b3"
                }
            }
        ]
    }
];</script>
lxkprmvk

lxkprmvk4#

以下是我的版本:

const res = data
  .flatMap((obj) => obj.fields)
  .map((obj) =>
    Object.entries(obj).reduce((p, [k, v]) => ({ ...p,
      [k]: v.value
    }), {})
  );

console.log(JSON.stringify(res, null, 4));
<script>
  const data = [{
      "fields": [{
        "field-1": {
          "id": "field-1",
          "value": "a1"
        },
        "field-2": {
          "id": "field-2",
          "value": "a2"
        },
        "field-3": {
          "id": "field-3",
          "value": "a3"
        }
      }]
    },
    {
      "fields": [{
        "field-1": {
          "id": "field-1",
          "value": "b1"
        },
        "field-2": {
          "id": "field-2",
          "value": "b2"
        },
        "field-3": {
          "id": "field-3",
          "value": "b3"
        }
      }]
    }
  ]
</script>

说明

我们首先flatMap()给定的数组,因为我们在数组中有数组,得到以下结果:

const x = data.flatMap((obj) => obj.fields);

输出:

[
    {
        "field-1": {
            "id": "field-1",
            "value": "a1"
        },
        "field-2": {
            "id": "field-2",
            "value": "a2"
        },
        "field-3": {
            "id": "field-3",
            "value": "a3"
        }
    },
    {
        "field-1": {
            "id": "field-1",
            "value": "b1"
        },
        "field-2": {
            "id": "field-2",
            "value": "b2"
        },
        "field-3": {
            "id": "field-3",
            "value": "b3"
        }
    }
]

现在对于上面数组中的每个对象,我只需要改变每个对象中每个键的值。因此,我们需要在前面的输出中调用map,并为每个对象使用reduce()生成我们想要的对象:

const res = x.map((obj) => Object.entries(obj).reduce((p, [k, v]) => ({ ...p, [k]: v.value }), {}))

输出:

[
    {
        "field-1": "a1",
        "field-2": "a2",
        "field-3": "a3"
    },
    {
        "field-1": "b1",
        "field-2": "b2",
        "field-3": "b3"
    }
]

相关问题