我想用一个技巧将Nr22_array_0和Nr22_array_1的所有信息放在一个循环函数的一个数组中。我认为在php中可以使用,但是我没有尝试过atm。只是我曾经做过一次的意思。在这种javascript情况下,它不起作用,因为看起来我写的所有内容都是错误的,因为他们将其视为非对象,请参见代码中的版本1-3输出。我不知道如何处理它来获得这样的输出:[“ apple”,“ food”,“ car”,“ test”,“ food”,“ car”,“ miau”]。忘了说那只是2的一个例子,但我需要N时间Nr22_array_N。
let Nr22_array_0 = ["apple","food","car"];
let Nr22_array_1 = ["test","food","car", "miau"];
let Nr22_array_new = new Array();
let Nr22_array_names = new Array();
let Nr22_i = 0;
let Nr22_to_run = 2;
function Nr22_pushArray( var_to_safe, x ){
var_to_safe.push.apply( var_to_safe, x );
}
while ( Nr22_i < Nr22_to_run ){
Nr22_array_names.push( "Nr22_array_" + Nr22_i );
Nr22_i++;
//console.log( "Nr22_array_names: ", Nr22_array_names );
//OUTPUT = OK: ["Nr22_array_0", "Nr22_array_1"]
}
Nr22_i = 0;
while ( Nr22_i < Nr22_to_run ){
// Version 1:
//Nr22_pushArray( Nr22_array_new, Nr22_array_names[Nr22_i] );
//console.log( "Nr22_array_new: ", Nr22_array_new[Nr22_i] );
// OUTPUT - NOK: CreateListFromArrayLike called on non-object
// Version 2:
//Nr22_pushArray( Nr22_array_new, "Nr22_array_" + [Nr22_i] );
//console.log( "Nr22_array_new: ", Nr22_array_new );
// OUTPUT - NOK: CreateListFromArrayLike called on non-object
//document.write(Nr22_array_names[Nr22_i]);
// Version 3:
//Nr22_pushArray( Nr22_array_new, Nr22_array_0 );
//console.log( "Nr22_array_new: ", Nr22_array_new );
// OUTPUT - NOK: sure 2x the same ["apple", "food", "car", "apple", "food", "car"]
Nr22_i++;
}
答案 0 :(得分:0)
只需使用concat()
:
let Nr22_array_0 = ["apple","food","car"];
let Nr22_array_1 = ["test","food","car", "miau"];
let myArr = Nr22_array_0.concat(Nr22_array_1);
console.log(myArr)
答案 1 :(得分:0)
您可以使用ES6传播算子从两个数组中形成一个新数组
const arr1 = ['foo', 'boo']
const arr2 = ['bar', 'gar']
const join = [...arr1, ...arr2]
console.log(join)
答案 2 :(得分:0)
let concatenatedArray = [].concat(Nr22_array_0, Nr22_array_1);
在这种情况下
[]
正在创建一个新数组,concat方法正在向该数组添加Nr22_array_0和Nr22_array_1的元素。
答案 3 :(得分:0)
const Nr22_array_0 = ["apple", "food", "car"];
const Nr22_array_1 = ["test", "food", "car", "miau"];
const res = Nr22_array_0
.concat(Nr22_array_1)
.reduce((acc,cur)=>{
if(!acc.includes(cur)){ acc.push(cur) };
return acc;
}, []);
console.log(res);
const Nr22_array_0 = ["apple", "food", "car"];
const Nr22_array_1 = ["test", "food", "car", "miau"];
const res = [...Nr22_array_0, ...Nr22_array_1.filter(item=>!Nr22_array_0.includes(item))]
console.log(res);
const Nr22_array_0 = ["apple", "food", "car"];
const Nr22_array_1 = ["test", "food", "car", "dog"];
const Nr22_array_2 = ["exam", "test", "car", "cat"];
const Nr22_array_3 = ["test", "apple", "machine", "world"];
function mergeNArrays() {
return Object.values(arguments).reduce((acc, cur) => {
acc.push(...cur.filter(item => !acc.includes(item)))
return acc;
}, []);
}
const res = mergeNArrays(
Nr22_array_0,
Nr22_array_1,
Nr22_array_2,
Nr22_array_3
);
console.log(res);
这是如何工作的?
const res = mergeNArrays(
Nr22_array_0,
Nr22_array_1,
Nr22_array_2,
Nr22_array_3
);
将所有要合并的数组传递到函数mergeNArrays
中。
function mergeNArrays() {
return Object.values(arguments).reduce((acc, cur) => {
acc.push(...cur.filter(item => !acc.includes(item)))
return acc;
}, []);
}
由于数组的数量不是固定的,因此请执行以下操作:
function mergeNArrays(arr1, arr2, arr3){}
是不可能的。
为解决这个问题,我们可以使用arguments来获取所有作为参数传递的数组。但是,它不是array
而是看起来像这样的object
:
{
'0': ["apple", "food", "car"],
'1': ["test", "food", "car", "dog"]
}
要将其转换为数组,我们需要使用Object.values(arguments);
并返回以下内容:
[
["apple", "food", "car"],
["test", "food", "car", "dog"]
]
现在,我们可以在以下哪些方面使用reduce
:
Object.values(arguments).reduce((acc, cur) => {
acc.push(...cur.filter(item => !acc.includes(item)))
return acc;
}, []);
acc
最初对应于空的[]
。对于reduce
的每次迭代,我们在cur
中添加["apple", "food", "car"]
中所有不存在的元素(例如:acc
)。
对于第一次迭代,acc
中不存在任何元素,因此cur.filter(/** etc **/)
返回以下内容:
["apple", "food", "car"]
但是,我们不想将其添加为acc的数组,而是添加为元素列表。展开操作...
的原因,该操作将数组转换为:
acc.push("apple", "food", "car");
这一直持续到我们遍历arguments
中的每个数组并添加了acc
中尚不存在的所有元素。
const Nr22_array_0 = ["apple", "food", "car"];
const Nr22_array_1 = ["test", "food", "car", "dog"];
const Nr22_array_2 = ["exam", "test", "car", "cat"];
const Nr22_array_3 = ["test", "apple", "machine", "world"];
const Nr22_array_names = [
"Nr22_array_0",
"Nr22_array_1",
"Nr22_array_2",
"Nr22_array_3",
]
function mergeNArrays(arrays) {
return arrays.reduce((acc, cur) => {
acc.push(...cur.filter(item => !acc.includes(item)))
return acc;
}, []);
}
const res = mergeNArrays(
Nr22_array_names.map(eval)
);
console.log(res);
答案 4 :(得分:0)
好家伙。首先,我想原谅我,因为我忘了说N个数组。第二大感谢@kemicofa给我这么大的答案,并提供一些很好的技巧来弄清楚我该如何处理。并对试图帮助不良编码人员的所有人也是如此。 (我知道,我知道。英语水平也可能会好一点= /)我不为那个代码感到骄傲,因为代码有点让人难以理解,而且我认为一个好的编码员可以写一个简短的版本,但它有效。 = D
let Nr22_array_0 = ["apple","food","car","tec"];
let Nr22_array_1 = ["test","food","car","tec", "miau"];
let Nr22_array_2 = ["bred","milk","suger", "water", "medicine"];
let Nr22_array_new = new Array();
let Nr22_array_names = new Array();
let Nr22_array_names_2 = new Array();
let Nr22_i = 0;
let Nr22_to_run = 3;
// create an Array with an list of all arraynames
while ( Nr22_i < Nr22_to_run ){
Nr22_array_names.push( "Nr22_array_" + Nr22_i );
Nr22_i++;
};
// complex function atm who has to make also the reduce and
// filter function for the strings, but it doesnt work for an
// maped array (should be written new!)
function mergeNArrays() {
return Object.values(arguments).reduce((acc, cur) => {
acc.push(...cur.filter(item => !acc.includes(item)))
return acc;
console.log("acc: ", acc);
}, []);
}
// create an maped object array
const res = mergeNArrays(
Nr22_array_names.map(eval)
);
// recursive function to read the object array and push the single values
// in an single string array
Nr22_i = 0;
Nr22_i_2 = 0;
while ( Nr22_i < res.length ){
Nr22_i_2 = 0;
while ( Nr22_i_2 < res[Nr22_i].length ){
Nr22_array_names_2.push( res[Nr22_i][Nr22_i_2] );
Nr22_i_2++;
}
Nr22_i++;
};
// reduce function for duplicate words
var NR22_uniq = Nr22_array_names_2.reduce(function( a, b ){
if (a.indexOf(b) < 0 ) a.push(b);
return a;
},[]);
NR22_uniq.sort();
document.write(NR22_uniq.join(", ") + ".<br>");
</script>