我想创建自己的函数,该函数将对象数组作为参数和要分组的键,然后根据指定的分组键创建索引的键值对。
例如:
var iris = [
{"Sepal_Length":1,"Sepal_Width":3.2, "Species":"setosa"},
{"Sepal_Length":1,"Sepal_Width":3.3, "Species":"viridis"},
{"Sepal_Length":1,"Sepal_Width":3.5, "Species":"virsicolor"},
{"Sepal_Length":2,"Sepal_Width":3.7, "Species":"setosa"},
{"Sepal_Length":1,"Sepal_Width":3.2, "Species":"viridis"},
{"Sepal_Length":2,"Sepal_Width":3.8, "Species":"virsicolor"}]
我想创建一个函数,该函数将允许我按物种分组以生成具有索引的新数组:
var iris = [
{"Sepal_Length":1,"Sepal_Width":3.2,"Species":"setosa", "index":1},
{"Sepal_Length":1,"Sepal_Width":3.3,"Species":"viridis", "index":2},
{"Sepal_Length":1,"Sepal_Width":3.5,"Species":"virsicolor", "index":3},
{"Sepal_Length":2,"Sepal_Width":3.7,"Species":"setosa", "index":1},
{"Sepal_Length":1,"Sepal_Width":3.2,"Species":"viridis", "index": 2},
{"Sepal_Length":2,"Sepal_Width":3.8,"Species":"virsicolor", "index": 3}]
我尝试使用map
和forEach
,但我对JS较新,因此感到有些困惑。任何帮助表示感谢,非常感谢!
答案 0 :(得分:1)
如果没有为此组设置索引,则可以将一个对象用作索引,并增加最大值。
var iris = [{ Sepal_Length: 1, Sepal_Width: 32, Species: "setosa" }, { Sepal_Length: 1, Sepal_Width: 32, Species: "viridis" }, { Sepal_Length: 1, Sepal_Width: 32, Species: "virsicolor" }, { Sepal_Length: 2, Sepal_Width: 32, Species: "setosa" }, { Sepal_Length: 1, Sepal_Width: 32, Species: "viridis" }, { Sepal_Length: 2, Sepal_Width: 32, Species: "virsicolor" }],
indices = Object.create(null),
max = 0,
result = iris.map(o => Object.assign(
{},
o,
{ index: indices[o.Species] = indices[o.Species] || ++max }
));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
您可以创建一个map
对象,该对象的每个唯一值都是键,而索引是值。 Destructure和key
分别获得属性的值和rest
。如果map
对象已经具有密钥,请使用它。否则,增加索引并向map
添加新密钥。
var iris = [{Sepal_Length:1,Sepal_Width:3.2,Species:"setosa"},{Sepal_Length:1,Sepal_Width:3.2,Species:"viridis"},{Sepal_Length:1,Sepal_Width:3.2,Species:"virsicolor"},{Sepal_Length:2,Sepal_Width:3.2,Species:"setosa"},{Sepal_Length:1,Sepal_Width:3.2,Species:"viridis"},{Sepal_Length:2,Sepal_Width:3.2,Species:"virsicolor"}];
function addIndex(array, key) {
let map = {}, index = 0;
return array.map(o => {
const value = o[key];
map[value] = map[value] || ++index;
return { ...o, index: map[value] }
})
}
console.log(addIndex(iris, 'Species'))
console.log(addIndex(iris, 'Sepal_Length'))
.as-console-wrapper { max-height: 100% !important; top: 0; }
如果要按多个键分组,则可以创建一个字符串,该字符串是由|
分隔的那些键的值的组合。
keys.map(k => o[k]).join('|')
现在,map
对象将具有参数中提到的键的每个唯一组合。这是一个片段:
var iris = [{Sepal_Length:1,Sepal_Width:3.2,Species:"setosa"},{Sepal_Length:1,Sepal_Width:3.2,Species:"viridis"},{Sepal_Length:1,Sepal_Width:3.2,Species:"virsicolor"},{Sepal_Length:2,Sepal_Width:3.2,Species:"setosa"},{Sepal_Length:1,Sepal_Width:3.2,Species:"viridis"},{Sepal_Length:2,Sepal_Width:3.2,Species:"virsicolor"}];
function addIndex(array, keys) {
let map = {}, index = 0;
return array.map(o => {
const partial = keys.map(k => o[k]).join('|');
map[partial] = map[partial] || ++index;
return { ...o, index: map[partial] }
})
}
console.log(addIndex(iris, ['Species', 'Sepal_Length']))
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:0)
您可以使用所有groupId创建一个对象,然后--
const groupObj = {
"setosa": "1",
"viridis": "2",
"virsicolor": "3",
};
iris.map((item) => {
item['index'] = groupObj[item.Species];
});
console.log(iris);