Javascript 如何将包含N个项目的数组转换为包含N个对象的数组
假设我们有这样一个数组:Javascript 如何将包含N个项目的数组转换为包含N个对象的数组,javascript,arrays,algorithm,Javascript,Arrays,Algorithm,假设我们有这样一个数组: const arr = [2,3,'red', 'white', ...]; // With n items 我想将该数组推入一个数组中,其中包含如下对象: [{a:2, b:3, c:'red', d:'white'}, {a: ..., b: ..., c: ...}, ...} 我从一个CSV文件中获取数据,该文件只提供了一个数组。我需要将这些值分配到一个对象中,每个对象有13个属性,因此每个对象看起来像: { a: ..., b: ..., c: ...,
const arr = [2,3,'red', 'white', ...]; // With n items
我想将该数组推入一个数组中,其中包含如下对象:
[{a:2, b:3, c:'red', d:'white'}, {a: ..., b: ..., c: ...}, ...}
我从一个CSV文件中获取数据,该文件只提供了一个数组。我需要将这些值分配到一个对象中,每个对象有13个属性,因此每个对象看起来像:
{ a: ..., b: ..., c: ..., d: ..., e: ..., f: ..., g: ...,
h: ..., i: ..., j: ...., k: ..., l: ..., m: ... }, ...
为了使这个操作尽可能简单,我伤了脑筋,但我需要帮助。你可以使用嵌套for循环来完成
const arr=[2,3,‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘红’、‘白’、‘3’、‘红’、‘白’、‘红’、‘白’、‘ss’、‘红’、‘白’ss',3',红色',白色',ss',3',红色',白色',ss',3',红色',白色',ss',3',红色',白色',ss'];
//用于存储结果的数组
var-res=[];
//迭代数组中的每13个元素
对于(变量i=0,i1=0;i console.log(res);
您可以使用嵌套for循环来完成
const arr=[2,3,‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘红’、‘白’、‘3’、‘红’、‘白’、‘红’、‘白’、‘ss’、‘红’、‘白’ss',3',红色',白色',ss',3',红色',白色',ss',3',红色',白色',ss',3',红色',白色',ss'];
//用于存储结果的数组
var-res=[];
//迭代数组中的每13个元素
对于(变量i=0,i1=0;i console.log(res);
如果要避免嵌套循环:
const arr = [2, 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss'];
resultArray = [];
tempObject = [];
for(i = 0; i < arr.length; i++) {
// Get a letter to use as a key
newKey = String.fromCharCode(97 + (i % 26));
// Push that onto the object
temp_obj[newKey] = arr[i];
// If you've done 13, put that into the array
if((i + 1) % 13 == 0) {
res_array.push(temp_obj);
temp_obj = {};
}
}
const arr=[2,3,‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘红’、‘白’、‘3’、‘红’、‘白’、‘红’、‘白’、‘ss’、‘红’、‘白’ss',3',红色',白色',ss',3',红色',白色',ss',3',红色',白色',ss',3',红色',白色',ss'];
结果射线=[];
tempObject=[];
对于(i=0;i
如果要避免嵌套循环:
const arr = [2, 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss', 3, 'red', 'white', 'ss'];
resultArray = [];
tempObject = [];
for(i = 0; i < arr.length; i++) {
// Get a letter to use as a key
newKey = String.fromCharCode(97 + (i % 26));
// Push that onto the object
temp_obj[newKey] = arr[i];
// If you've done 13, put that into the array
if((i + 1) % 13 == 0) {
res_array.push(temp_obj);
temp_obj = {};
}
}
const arr=[2,3,‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘3’、‘红’、‘白’、‘ss’、‘红’、‘白’、‘3’、‘红’、‘白’、‘红’、‘白’、‘ss’、‘红’、‘白’ss',3',红色',白色',ss',3',红色',白色',ss',3',红色',白色',ss',3',红色',白色',ss'];
结果射线=[];
tempObject=[];
对于(i=0;i
你所说的“将对象推入一个数组”是什么意思?你能更清楚一点函数应该做什么吗?为什么c是白色的?@PetarPetrovic只是随机值Petar。我在尝试时伤了脑筋
-看起来你实际上没有尝试过任何东西all@MBoPranav C Balan有正确的方法。你所说的“将对象推入数组”是什么意思?你能更清楚一点函数应该做什么吗?为什么c是白色的?@PetarPetrovic只是随机值Petar。我在尝试时伤了脑筋
-看起来你实际上没有尝试过任何东西all@MBoPranav C Balan的方法正确。(索引+10)。toString(36)
-尼斯(索引+10)。toString(36)
-不错