[英]Strip key values out of object array with javascript
I have an array with this data in it: 我有一个数组,其中包含以下数据:
[
{
user1post2: {
ad:"Car", ac:39500, af:"4", ah:"klgjoirt3904d", ab:"Acura",
ae:"2013 ACURA MDX 3.5L V6 AWD 6AT (294 HP)", ag:"Mint", aa:"Option2"
},
user1post1: {
ad:"Truck", ac:6799, af:"3", ah:"ldkfldfjljKey", ab:"GMC",
ae:"1/2 Ton with plow", ag:"Mint Looks", aa:"Option1"
}
}
]
I need to strip out user1post2
and user1post1
. 我需要删除user1post2
和user1post1
。
I've tried using something like this, but it doesn't work: 我已经尝试过使用类似的方法,但是它不起作用:
var valu2 = values; //put current array into new variable `valu2`
console.log(valu2.toSource()); //Log to console to verify variable
var finalData = []; //Create new array
for (var name2 in valu2) {
finalData.push(valu2[name2]);
};
console.log(finalData.toSource());
How do I strip out those key values user1post2:
? 如何删除那些关键值user1post2:
When I check the length, 当我检查长度时,
console.log("length is:" + values.length);
it indicates a length of 1
, so if the count starts at zero, then that would be correct. 它表示长度为1
,因此,如果计数从零开始,那将是正确的。 I could set up a for loop that iterates from 0 to i. 我可以设置一个从0迭代到i的for循环。
I'm not sure what the syntax or property is to reference data inside the array. 我不确定引用数组内部数据的语法或属性是什么。
You have an array with a single object within. 您有一个包含单个对象的数组。 The keys of that object are what you want as an array. 该对象的键是您想要作为数组的键。 So: 所以:
var val = [{ /*... your data ...*/ }];
var out = [];
var myBigObject = val[0];
for (var i in myBigObject) {
if (myBigObject.hasOwnProperty(i)) {
out.push(myBigObject[i]);
}
}
console.log(out);
Is this what you want? 这是你想要的吗?
values = [{
user1post2: {
ad: "Car",
ac: 39500,
af: "4",
ah: "klgjoirt3904d",
ab: "Acura",
ae: "2013 ACURA MDX 3.5L V6 AWD 6AT (294 HP)",
ag: "Mint",
aa: "Option2"
},
user1post1: {
ad: "Truck",
ac: 6799,
af: "3",
ah: "ldkfldfjljKey",
ab: "GMC",
ae: "1/2 Ton with plow",
ag: "Mint Looks",
aa: "Option1"
}
}]
out = [];
for(prop in values[0]) {
out.push(prop);
}
console.log(out);
or are you trying to iterate over the actual data inside each property (ie loop over user1post2 to get the value ad: "Car" etc)? 还是您要遍历每个属性内的实际数据(即循环遍历user1post2以获得值广告:“ Car”等)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.