如何将两个特定的对象值转换为一个元组项数组?



如何转换以下对象:

{
Articul: ["0092T30560", "0092T30790", "0092T30800"],
Brand: ["Bosch", "Bosch", "Bosch"],
}

到以下数组:

[
["0092T30560", "Bosch"],
["0092T30790", "Bosch"],
["0092T30800", "Bosch"],
]

之后我可以将它的元素引用为

array[0],得到["0092T30560", "Bosch"]

array[1][0],得到"0092T30790",等等?

您至少要尝试一些东西,无论如何您可以按照以下方法来做:

  • 使用object-destructuring获取物品和品牌
  • 遍历articles获取value作为key,并根据articles的key从Brands数组中获取其值。
obj = {
Articul: ["0092T30560", "0092T30790", "0092T30800"],
Brand: ["Bosch", "Bosch", "Bosch"],
}
let {Articul: Articuls, Brand: Brands} = obj;
let array = Articuls.map((Articul, key)=>[Articul, Brands[i]]);

结果:

array[0] // ["0092T30560", "Bosch"]
array[1][0] //0092T30790

const object = {
Articul: ["0092T30560", "0092T30790", "0092T30800"],
Brand: ["Bosch", "Bosch", "Bosch"],
};
const modified_obj = object.Articul.map((item, index) => {
return [
item,
object.Brand[index],
];
});
console.log(modified_obj);

在从一个对象中检索到两个数组后,OP想要将两个数组合并为一个元组项数组,其中每个元组都是来自两个源数组的相等位置项的数组。

下一个提供的可能解决方案的第一部分利用解构赋值重命名…

const sampleData = {
Articul: ["0092T30560", "0092T30790", "0092T30800"],
Brand: ["Bosch", "Bosch", "Bosch"],
};
const {
Articul: articleIdList,
Brand: brandNameList,
} = sampleData;
console.log({
articleIdList,
brandNameList,
});
.as-console-wrapper { min-height: 100%!important; top: 0; }

将上述两个数组合并为一个数组的步骤可以通过mapreduce任务来实现。

对于映射,可以利用map的第二个thisArg参数,这样在获得处理数组的当前itemidx的同时,还可以通过回调thiscontext访问第二个数组…

const sampleData = {
Articul: ["0092T30560", "0092T30790", "0092T30800"],
Brand: ["Bosch", "Bosch", "Bosch"],
};
const {
Articul: articleIdList,
Brand: brandNameList,
} = sampleData;
function createTupleFromBoundList (item, idx) {
// `item` ... current item of `articleIdList`.
// `idx` ... position of the currently processed item.
// `this` ... `brandNameList` provided as `thisArg`.
return [item, this[idx]];
}
const articleAndBrandTupleList =
articleIdList.map(createTupleFromBoundList, brandNameList);
console.log({ articleAndBrandTupleList });

// ... or, since the approach is known now, the direct notation ... ...
console.log(`sampleData.Articul
.map(createTupleFromBoundList, sampleData.Brand) ...`,
sampleData.Articul
.map(createTupleFromBoundList, sampleData.Brand)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

对于基于reduce的方法,需要提供第二个数组作为收集器/累加器对象的一部分,该对象将作为reducer函数的初始值传递…

const sampleData = {
Articul: ["0092T30560", "0092T30790", "0092T30800"],
Brand: ["Bosch", "Bosch", "Bosch"],
};
const {
Articul: articleIdList,
Brand: brandNameList,
} = sampleData;
function createAndCollectTuple(collector, item, idx) {
// `item` ... current item of `articleIdList`.
// `idx` ... position of the currently processed item.
collector.result.push([
item,
collector.secondList[idx],
]);
return collector;
}
const articleAndBrandTupleList =
articleIdList.reduce(createAndCollectTuple, {
secondList: brandNameList,
result: [],
}).result;
console.log({ articleAndBrandTupleList });

// ... or, since the approach is known now, the direct notation ...
console.log(`sampleData.Articul
.reduce(createAndCollectTuple, {
secondList: sampleData.Brand,
result: [],
}).result ...`,
sampleData.Articul
.reduce(createAndCollectTuple, {
secondList: sampleData.Brand,
result: [],
}).result
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

最新更新