我正在处理大量的对象。我已将我的数据结构简化为以下内容。每个对象都有一个
id
以及每个
有0到4个数组与其关联。数组名称是动态生成的,最多有2个元素,可以是基于其他要求的任意数量的名称。我的初始对象如下所示:
const arr = [{id: "12345", array1: ["Banana", "Apple"], array2: ["Orange", "Strawberry"]},
{id: "12345", array0: ["Potato", "Tomato"]},
{id: "54321", array0: ["Kiwi", "Apple"], array1: ["Potato", "Onion"]},
{id: "54321", array2: ["Orange", "Tomato"], array0: ["Kiwi", "Banana"]},
{id: "13579", array1: ["Banana", "Apple"], array2: ["Grapefruit", "Onion"]},
{id: "13579", array1: ["Potato", "Banana"], array2: ["Orange", "Pepper"]}]
身份证件
和一个
type
const lookup = [{id: "12345", type: "Banana"},
{id: "12345", type: "Kiwi"},
{id: "12345", type: "Apple"},
{id: "54321", type: "Strawberry"}
{id: "54321", type: "Tomato"},
{id: "54321", type: "Banana"},
{id: "13579", type: "Tomato"},
{id: "13579", type: "Grapefruit"}]
类型
在任何相应的
id's
const result = [{id: "12345", array2: ["Orange", "Strawberry"]},
{id: "12345", array0: ["Potato", "Tomato"]},
{id: "54321", array0: ["Kiwi", "Apple"], array1: ["Potato", "Onion"]},
{id: "54321"},
{id: "13579", array1: ["Banana", "Apple"]},
{id: "13579", array1: ["Potato", "Banana"], array2: ["Orange", "Pepper"]}]
我被挂起的部分是没有提前知道对象键的名称,以及如何用
lookup
对象的类型属性。我最初的想法是
Object.values