数组的复杂问题
Complicated issue with arrays
我有一个复杂的问题需要解决。
我有一个如下所示的数组:
var array = [
["1hszZhsez7", 0, "mb12", 0],
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
["coosihchw9", 0, "zz00", 0],
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1],
["caosw8ncxc", 2, "uu33", 0]
];
这个数组总是有不同的值。那么让我们进入吧。
该数组基本上具有以下值:
var array = [
[id, type, code, priority]
];
id: id是唯一的。没什么特别的。
type: 共有三种不同的类型。 0、1 和 2。
代码:代码不是唯一的。多个数组项可以相同。
priority: 优先级是一个数字。如果数字低,则优先级高。数字大则优先级低
任务:
首先,我们需要将不同的类型排序到不同的数组中。所以它看起来像这样:
var type_zero = [
["coosihchw9", 0, "zz00", 0]
];
var type_one = [
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1]
];
var type_two = [
["caosw8ncxc", 2, "uu33", 0]
];
这个函数很简单:
array.forEach(function(item) {
if (item[1] === 0) {
type_zero.push([item[0], item[1], item[2], item[3], item[4], item[5]]);
}
else if (item[1] === 1) {
type_one.push([item[0], item[1], item[2], item[3], item[4], item[5]]);
}
else if (item[1] === 2) {
type_two.push([item[0], item[1], item[2], item[3], item[4], item[5]]);
}
});
但接着就开始头疼了。我们需要为每个代码组创建一个单独的数组。但我们永远不知道有多少不同的代码。所以我们不能制作静态数组。
无论我们使用哪种代码。最后,它应该看起来像这样,基于我们的示例数组。
var type_zero_zz00 = [
["coosihchw9", 0, "zz00", 0]
];
var type_one_tt43 = [
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
];
var type_one_uu33 = [
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1]
];
var type_two_uu33 = [
["caosw8ncxc", 2, "uu33", 0]
];
为什么我需要这些数组?在下一步中,我必须为每个数组列表执行一个 forEach 循环才能继续。我希望有人知道解决方案,因为这个问题困扰了很多天,而且不可能的想法越来越大。
提前致谢,
马吕斯
我看到你只需要创建函数来获取你需要的信息,而不是创建很多不同的变量。
以下是您需要完成的事情的想法,而不是您需要的 100% 准确版本
var array = [
["1hszZhsez7", 0, "mb12", 0],
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
["coosihchw9", 0, "zz00", 0],
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1],
["caosw8ncxc", 2, "uu33", 0]
];
function getByCode(code){
return array.filter( item => item[1] === code);
}
function getByGroup(group){
return array.filter( item => item[2] === group);
}
您可以使用单个数组,只使用代码作为数组索引。
var output = [];
var array = [
["1hszZhsez7", 0, "mb12", 0],
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
["coosihchw9", 0, "zz00", 0],
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1],
["caosw8ncxc", 2, "uu33", 0]
];
array.forEach(function(item) {
if (output[item[1]] == undefined) {
output[item[1]] = []
}
output[item[1]].push(item);
});
console.log(0, output[0])
console.log(1, output[1])
console.log(2, output[2])
使用 two-dimensional 字典怎么样?通过这样做,您最终会得到这样的数据结构,您可以在其中为类型和代码组添加新值(它们本身就是字典):
var type = {};
type["0"]["zz00"] = [
["coosihchw9", 0, "zz00", 0]
];
type["1"]["tt43"] = [
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
];
type["1"]["uu33"] = [
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1]
];
type["2"]["uu33"] = [
["caosw8ncxc", 2, "uu33", 0]
];
创建处理动态代码的地图:
function groupArray(arr) {
const map = {};
arr.forEach(element => {
const [id, type, code, priority] = element;
map[code] = Array.isArray(map[code]) ? [...map[code], element] : [element];
});
return map;
}
const type_zero_group = groupArray(type_zero)
const type_one_group = groupArray(type_one)
const type_two_group = groupArray(type_two)
现在,如果类型本身变得动态,也为它们创建一个映射器。 Two-level 张地图。
不是为每个类型和代码的组合创建一个新变量,而是为每个组合创建一个带有 属性 的对象。现在,它实际上是一个简单的两键分组问题。您可以使用 Array.reduce()
,并生成对象或地图:
const array = [["1hszZhsez7",0,"mb12",0],["87hJKLi893",1,"tt43",0],["jchd79dcic",1,"tt43",2],["as0w9ejasm",1,"tt43",1],["coosihchw9",0,"zz00",0],["vs0x0j9ndm",1,"uu33",0],["00s0kd20r7",1,"uu33",2],["xassdwddao",1,"uu33",1],["caosw8ncxc",2,"uu33",0]];
const types = ['zero', 'one', 'two'];
const result = array.reduce((r, row) => {
const [, type, code] = row;
const key = `type_${types[type]}_${code}`;
if(!r[key]) r[key] = [];
r[key].push(row);
return r;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
你构建一个这样的对象来解决你的问题,而不是试图制造很多变量。
const results = {
0: {},
1: {},
2: {},
};
for (let item of array) {
let [id, type, code, priority] = item;
if (results[type][code] === undefined) {
results[type][code] = [item];
} else {
results[type][code] = results[type][code].concat([item]);
}
}
然后您可以像这样映射这些对象:
for (let type of Object.keys(results)) {
for (let code of Object.keys(results[type])) {
const array = results[type][code];
}
}
您可以使用 reduce
方法和对象作为累加器来执行此操作,您可以使用 type-code
作为键对值进行分组。
var array = [
["1hszZhsez7", 0, "mb12", 0],
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
["coosihchw9", 0, "zz00", 0],
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1],
["caosw8ncxc", 2, "uu33", 0]
];
const result = array.reduce((r, e) => {
const [id, type, code, priority] = e;
const key = `${type}|${code}`;
if (!r[key]) {
r[key] = []
}
r[key].push(e);
return r;
}, {})
console.log(Object.values(result))
我有一个复杂的问题需要解决。
我有一个如下所示的数组:
var array = [
["1hszZhsez7", 0, "mb12", 0],
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
["coosihchw9", 0, "zz00", 0],
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1],
["caosw8ncxc", 2, "uu33", 0]
];
这个数组总是有不同的值。那么让我们进入吧。
该数组基本上具有以下值:
var array = [
[id, type, code, priority]
];
id: id是唯一的。没什么特别的。
type: 共有三种不同的类型。 0、1 和 2。
代码:代码不是唯一的。多个数组项可以相同。
priority: 优先级是一个数字。如果数字低,则优先级高。数字大则优先级低
任务:
首先,我们需要将不同的类型排序到不同的数组中。所以它看起来像这样:
var type_zero = [
["coosihchw9", 0, "zz00", 0]
];
var type_one = [
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1]
];
var type_two = [
["caosw8ncxc", 2, "uu33", 0]
];
这个函数很简单:
array.forEach(function(item) {
if (item[1] === 0) {
type_zero.push([item[0], item[1], item[2], item[3], item[4], item[5]]);
}
else if (item[1] === 1) {
type_one.push([item[0], item[1], item[2], item[3], item[4], item[5]]);
}
else if (item[1] === 2) {
type_two.push([item[0], item[1], item[2], item[3], item[4], item[5]]);
}
});
但接着就开始头疼了。我们需要为每个代码组创建一个单独的数组。但我们永远不知道有多少不同的代码。所以我们不能制作静态数组。
无论我们使用哪种代码。最后,它应该看起来像这样,基于我们的示例数组。
var type_zero_zz00 = [
["coosihchw9", 0, "zz00", 0]
];
var type_one_tt43 = [
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
];
var type_one_uu33 = [
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1]
];
var type_two_uu33 = [
["caosw8ncxc", 2, "uu33", 0]
];
为什么我需要这些数组?在下一步中,我必须为每个数组列表执行一个 forEach 循环才能继续。我希望有人知道解决方案,因为这个问题困扰了很多天,而且不可能的想法越来越大。
提前致谢,
马吕斯
我看到你只需要创建函数来获取你需要的信息,而不是创建很多不同的变量。
以下是您需要完成的事情的想法,而不是您需要的 100% 准确版本
var array = [
["1hszZhsez7", 0, "mb12", 0],
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
["coosihchw9", 0, "zz00", 0],
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1],
["caosw8ncxc", 2, "uu33", 0]
];
function getByCode(code){
return array.filter( item => item[1] === code);
}
function getByGroup(group){
return array.filter( item => item[2] === group);
}
您可以使用单个数组,只使用代码作为数组索引。
var output = [];
var array = [
["1hszZhsez7", 0, "mb12", 0],
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
["coosihchw9", 0, "zz00", 0],
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1],
["caosw8ncxc", 2, "uu33", 0]
];
array.forEach(function(item) {
if (output[item[1]] == undefined) {
output[item[1]] = []
}
output[item[1]].push(item);
});
console.log(0, output[0])
console.log(1, output[1])
console.log(2, output[2])
使用 two-dimensional 字典怎么样?通过这样做,您最终会得到这样的数据结构,您可以在其中为类型和代码组添加新值(它们本身就是字典):
var type = {};
type["0"]["zz00"] = [
["coosihchw9", 0, "zz00", 0]
];
type["1"]["tt43"] = [
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
];
type["1"]["uu33"] = [
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1]
];
type["2"]["uu33"] = [
["caosw8ncxc", 2, "uu33", 0]
];
创建处理动态代码的地图:
function groupArray(arr) {
const map = {};
arr.forEach(element => {
const [id, type, code, priority] = element;
map[code] = Array.isArray(map[code]) ? [...map[code], element] : [element];
});
return map;
}
const type_zero_group = groupArray(type_zero)
const type_one_group = groupArray(type_one)
const type_two_group = groupArray(type_two)
现在,如果类型本身变得动态,也为它们创建一个映射器。 Two-level 张地图。
不是为每个类型和代码的组合创建一个新变量,而是为每个组合创建一个带有 属性 的对象。现在,它实际上是一个简单的两键分组问题。您可以使用 Array.reduce()
,并生成对象或地图:
const array = [["1hszZhsez7",0,"mb12",0],["87hJKLi893",1,"tt43",0],["jchd79dcic",1,"tt43",2],["as0w9ejasm",1,"tt43",1],["coosihchw9",0,"zz00",0],["vs0x0j9ndm",1,"uu33",0],["00s0kd20r7",1,"uu33",2],["xassdwddao",1,"uu33",1],["caosw8ncxc",2,"uu33",0]];
const types = ['zero', 'one', 'two'];
const result = array.reduce((r, row) => {
const [, type, code] = row;
const key = `type_${types[type]}_${code}`;
if(!r[key]) r[key] = [];
r[key].push(row);
return r;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
你构建一个这样的对象来解决你的问题,而不是试图制造很多变量。
const results = {
0: {},
1: {},
2: {},
};
for (let item of array) {
let [id, type, code, priority] = item;
if (results[type][code] === undefined) {
results[type][code] = [item];
} else {
results[type][code] = results[type][code].concat([item]);
}
}
然后您可以像这样映射这些对象:
for (let type of Object.keys(results)) {
for (let code of Object.keys(results[type])) {
const array = results[type][code];
}
}
您可以使用 reduce
方法和对象作为累加器来执行此操作,您可以使用 type-code
作为键对值进行分组。
var array = [
["1hszZhsez7", 0, "mb12", 0],
["87hJKLi893", 1, "tt43", 0],
["jchd79dcic", 1, "tt43", 2],
["as0w9ejasm", 1, "tt43", 1],
["coosihchw9", 0, "zz00", 0],
["vs0x0j9ndm", 1, "uu33", 0],
["00s0kd20r7", 1, "uu33", 2],
["xassdwddao", 1, "uu33", 1],
["caosw8ncxc", 2, "uu33", 0]
];
const result = array.reduce((r, e) => {
const [id, type, code, priority] = e;
const key = `${type}|${code}`;
if (!r[key]) {
r[key] = []
}
r[key].push(e);
return r;
}, {})
console.log(Object.values(result))