gpt4 book ai didi

javascript - 当对象可以具有不同类型时如何按键对对象数组进行分组

转载 作者:行者123 更新时间:2023-12-02 02:04:25 26 4
gpt4 key购买 nike

我有一个像这样的对象数组:

const input = [
{
"name" : "car",
"sign" : "+",
"options" : "benz"
},
{
"name" : "bike",
"sign" : "+",
"options" : "pulsar"
},
{
"name" : "bike",
"sign" : "+",
"options" : "enfield"
},
{
"name" : "car",
"sign" : "+",
"options" : ["toyota","hyundai","benz"]
},
{
"name" : "",
"sign" : "",
"options" : ""
},
{
"name" : "car",
"sign" : "+",
"options" : ["audi", "ford"]
}
]

输入结构是这样的:每个对象有3个键值对。

  1. 全部可以为空

  2. 它可以有有效值:

    a.选项可以是字符串

    b.或者它可以是一个数组

我不想在输出中添加值为“”(空字符串)的对象,输出应该是这样的:

[
{"car" : [ "benz", "toyota","hyundai", "audi", "ford"]},
{"bike" : ["pulsar","enfield" ]}
]

其他各种答案没有显示当对象值可以是数组或字符串时如何处理,所以我正在写一个新问题。

我不确定是否必须使用reduce或map。非常感谢任何帮助,提前致谢。

最佳答案

这是一个相当标准的“分组依据”操作,按一个属性进行分组并将另一个属性累加到一个数组中。

为了容纳字符串和数组,您使用 concat()options 值添加到累加器中。 Concat 接受单个值或值数组,并将后者展平到目标数组中。为了避免重复,您可以使用 Set如果 nameoptions 都为 true,下面的代码片段只会向累加器添加元素。

const input = [
{ "name": "", "sign": "+", "options": "benz" }, // blank 'name' property
{ "name": "bike", "sign": "+", "options": "" }, // blank 'options' property
{ "name": "bike", "sign": "+", "options": "enfield" },
{ "name": "car", "sign": "+", "options": ["toyota", "hyundai"] },
{ "name": "car", "sign": "", "options": "ford" },
{ "name": "car", "sign": "+", "options": ["audi", "ford"] }]

const result = Object.values(
input.reduce((acc, { name, options }) => {
if (name && options) {
const category = (acc[name] || (acc[name] = { [name]: [] }));
category[name] = [...new Set(category[name].concat(options))];
}
return acc;
}, {}));

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

或者,为了避免在每次迭代时创建新的 Set,您可以累积到一个 Set 中,然后映射 reduce() 调用的结果,并将每个集合转换为数组。

const input = [
{ "name": "", "sign": "+", "options": "benz" }, // blank 'name' property
{ "name": "bike", "sign": "+", "options": "" }, // blank 'options' property
{ "name": "bike", "sign": "+", "options": "enfield" },
{ "name": "car", "sign": "+", "options": ["toyota", "hyundai"] },
{ "name": "car", "sign": "", "options": "ford" },
{ "name": "car", "sign": "+", "options": ["audi", "ford"] }]

const result = Object.entries(
input.reduce((acc, { name, options }) => {
if (name && options) {
acc[name] || (acc[name] = new Set);
[].concat(options).forEach(option => acc[name].add(option));
}
return acc;
}, {}))
.map(([name, options]) => ({ [name]: [...options] }));

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

由于您似乎无法使用 ES6 迭代器,因此这里有一个选项,仅使用 Array#includes() 检查以避免重复。

const input = [
{ "name": "", "sign": "+", "options": "benz" }, // blank 'name' property
{ "name": "bike", "sign": "+", "options": "" }, // blank 'options' property
{ "name": "bike", "sign": "+", "options": "enfield" },
{ "name": "car", "sign": "+", "options": ["toyota", "hyundai"] },
{ "name": "car", "sign": "", "options": "ford" },
{ "name": "car", "sign": "+", "options": ["audi", "ford"] }]

const result = Object.values(
input.reduce((acc, { name, options }) => {
if (name && options) {
const category = (acc[name] || (acc[name] = { [name]: [] }));
[].concat(options).forEach(option => {
if (!category[name].includes(option)) category[name].push(option)
});
}
return acc;
}, {}));

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

ES3

var input = [
{ "name": "", "sign": "+", "options": "benz" },
{ "name": "bike", "sign": "+", "options": "" },
{ "name": "bike", "sign": "+", "options": "enfield" },
{ "name": "car", "sign": "+", "options": ["toyota", "hyundai"] },
{ "name": "car", "sign": "", "options": "ford" },
{ "name": "car", "sign": "+", "options": ["audi", "ford"] }
];

var temp = {};

for (var i = 0; i < input.length; i++) {
var name_ = input[i].name;
var options_ = input[i].options;

if (name_ !== undefined && name_ !== "" && options_ !== undefined && options_ !== "") {

if (!temp.hasOwnProperty(name_)) {
temp[name_] = {};
temp[name_][name_] = [];
}

var options_ = [].concat(options_);

for (var j = 0; j < options_.length; j++) {
if (!temp[name_][name_].includes(options_[j])) {
temp[name_][name_].push(options_[j]);
}
}

}
}

var result = [];

for (var key_ in temp) {
if (temp.hasOwnProperty(key_)) {
result.push(temp[key_]);
}
}

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

关于javascript - 当对象可以具有不同类型时如何按键对对象数组进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68640742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com