I have an array of values and an object where the values are smaller arrays:
我有一个值数组和一个值为较小数组的对象:
array = [1, 2, 3, 4, 2]
object = {
gender: [male, female],
grade: [7th, 8th, 9th],
}
I want to zip the array and the object so that the values in the array are assigned to the new objects that are keyed with the values in the object, like this:
我想压缩数组和对象,以便将数组中的值分配给使用对象中的值键入的新对象,如下所示:
targetObject = {
gender: [
male: 1,
female: 2,
],
grade: [
7th: 3,
8th: 4,
9th: 2,
],
}
My first stab is to iterate through the object and create a new array
我的第一个尝试是遍历对象并创建一个新数组
var newArray = [];
for(key in object) {
for(i=0;i<key.length;i++){
newArray.push(key[i]);
}
}
Then zip them together
然后把它们拉在一起
var newObject = {};
for (var i = 0; i < newArray.length; i++) {
newObject[newArray[i]] = array[i];
}
If my syntax is write I believe I'm here:
如果我的语法是写的,我相信我在这里:
array == [1, 2, 3, 4, 2]
object == {
gender: [male, female],
grade: [7th, 8th, 9th],
}
newArray == [male, female, 7th, 8th, 9th]
newObject == {
male: 1,
female: 2,
7th: 3,
8th: 4,
9th: 2,
}
It looks like I'm close, but I also feel like I'm stringing together a bunch of fragile code. Is there a better way? And if not, how do I go from my newObject to my targetObject
看起来我很接近,但我也觉得我正在把一堆脆弱的代码串在一起。有没有更好的办法?如果没有,我如何从newObject转到targetObject
2 个解决方案
#1
0
Properties of objects have no order. But if the order in not important, the I propose this solution:
对象的属性没有顺序。但如果订单不重要,我建议这个解决方案:
var array = [1, 2, 3, 4, 2],
object = {
gender: ['male', 'female'],
grade: ['7th', '8th', '9th'],
},
newObject = {},
i = 0;
Object.keys(object).forEach(function (a) {
newObject[a] = newObject[a] || {};
object[a].forEach(function (b) {
newObject[a][b] = array[i];
i++;
});
});
document.write('<pre>' + JSON.stringify(newObject, 0, 4) + '</pre>');
For an order proof object, I suggest to use arrays in combination with objects.
对于订单证明对象,我建议将数组与对象结合使用。
var array = [1, 2, 3, 4, 2],
object = [
{ gender: ['male', 'female'] },
{ grade: ['7th', '8th', '9th'] }
],
newObject = {},
i = 0;
object.forEach(function (a) {
Object.keys(a).forEach(function (b) {
newObject[b] = newObject[b] || {};
a[b].forEach(function (c) {
newObject[b][c] = array[i];
i++;
});
});
});
document.write('<pre>' + JSON.stringify(newObject, 0, 4) + '</pre>');
#2
0
The following Snippet creates the target object, and it will work in most browsers.
以下代码段创建目标对象,它可以在大多数浏览器中使用。
However, note that object keys are not guaranteed to be ordered. So the output could potentially be this:
但请注意,不保证订购对象键。所以输出可能是这样的:
targetObject = {
grade: [
7th: 1,
8th: 2,
9th: 3,
],
gender: [
male: 4,
female: 2,
]
}
Snippet:
var array = [1, 2, 3, 4, 2],
object = {
gender: ['male', 'female'],
grade: ['7th', '8th', '9th']
},
targetObject= {},
i,
j,
k= 0;
for(var i in object) {
targetObject[i]= targetObject[i] || {}; //initialize if needed
object[i].forEach(function(key) { //iterate through the keys
targetObject[i][key]= array[k++]; //assign to the next array element
});
}
document.querySelector('pre').textContent= JSON.stringify(targetObject, 0, 2); //show targetObject
<pre></pre>
#1
0
Properties of objects have no order. But if the order in not important, the I propose this solution:
对象的属性没有顺序。但如果订单不重要,我建议这个解决方案:
var array = [1, 2, 3, 4, 2],
object = {
gender: ['male', 'female'],
grade: ['7th', '8th', '9th'],
},
newObject = {},
i = 0;
Object.keys(object).forEach(function (a) {
newObject[a] = newObject[a] || {};
object[a].forEach(function (b) {
newObject[a][b] = array[i];
i++;
});
});
document.write('<pre>' + JSON.stringify(newObject, 0, 4) + '</pre>');
For an order proof object, I suggest to use arrays in combination with objects.
对于订单证明对象,我建议将数组与对象结合使用。
var array = [1, 2, 3, 4, 2],
object = [
{ gender: ['male', 'female'] },
{ grade: ['7th', '8th', '9th'] }
],
newObject = {},
i = 0;
object.forEach(function (a) {
Object.keys(a).forEach(function (b) {
newObject[b] = newObject[b] || {};
a[b].forEach(function (c) {
newObject[b][c] = array[i];
i++;
});
});
});
document.write('<pre>' + JSON.stringify(newObject, 0, 4) + '</pre>');
#2
0
The following Snippet creates the target object, and it will work in most browsers.
以下代码段创建目标对象,它可以在大多数浏览器中使用。
However, note that object keys are not guaranteed to be ordered. So the output could potentially be this:
但请注意,不保证订购对象键。所以输出可能是这样的:
targetObject = {
grade: [
7th: 1,
8th: 2,
9th: 3,
],
gender: [
male: 4,
female: 2,
]
}
Snippet:
var array = [1, 2, 3, 4, 2],
object = {
gender: ['male', 'female'],
grade: ['7th', '8th', '9th']
},
targetObject= {},
i,
j,
k= 0;
for(var i in object) {
targetObject[i]= targetObject[i] || {}; //initialize if needed
object[i].forEach(function(key) { //iterate through the keys
targetObject[i][key]= array[k++]; //assign to the next array element
});
}
document.querySelector('pre').textContent= JSON.stringify(targetObject, 0, 2); //show targetObject
<pre></pre>