Reputation: 16469
I have an array of objects
var j = [{"v1":["1","2","3"]}, {"v2":["4","5","6"]}, {"v3":["7","8","9"]}];
I want to check against the object property and perform some sort of logic. I am new to JS so I'm not sure of all the methods I have access to. Basically I want to compare the key value of the object to a string. If the key and the string are the same, then I would remove that object from the array. I'm not sure how to iterate through the object's key in the array.
var str = "v1";
for (var i in j) {
if (i.key == str) { // not sure how to access key value
j.splice(i,1);
}
}
Upvotes: 0
Views: 122
Reputation: 707696
As we've been discussing in comments, there is seldom a reason to use an array of objects that each only have one property (unless you're using the array to maintain a specific order), so I thought perhaps your problem might be easier if the data was structured like this:
var j = {"v1":["1","2","3"], "v2":["4","5","6"], "v3":["7","8","9"]};
And, you could then iterate it like this:
for (var key in j) {
console.log("j[" + key + "] = ", j[key]);
}
Working demo: http://jsfiddle.net/jfriend00/qdgzso1g/
Upvotes: 1
Reputation: 148624
Try this :
var j = [{"v1":["1","2","3"]}, {"v2":["4","5","6"]}, {"v3":["7","8","9"]}];
for (var i = 0; i < j.length; i++)
{
for (var k in j[i]) //do use this if you need to iterate
{
if (k === "mySomething")
{
//...do your stuff
}
}
}
Edit
less verbose :
for (var i = 0; i < j.length; i++)
{
if ("mySomething" in j[i])
{
//...do your stuff
}
}
Upvotes: 2