0

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);
  }
}
8
  • 1
    Look into for loops (for the array) and for in (for the objects). Commented Oct 24, 2014 at 20:01
  • 1
    You should never iterate an array with for (var i in j) because that iterates all enumerable properties of the array, not just array elements. You can use either for (var i = 0; i < j.length; i++) or you can use j.forEach(function(val, index) {...}). The construct for (var i in j) is used to iterate all properties of an object. Commented Oct 24, 2014 at 20:01
  • 1
    Whenever I see this sort of data structure, I always wonder if the data structure is just wrong and it should be: var j = {"v1":["1","2","3"], "v2":["4","5","6"], "v3":["7","8","9"]}; so you can iterate the properties of j directly and skip the array entirely. If the properties are all meant to be unique and not in any particular order, this is a better way to structure the data. Commented Oct 24, 2014 at 20:04
  • @jfriend00 I'm new to JS so I am a bit confused on how ot construct my data at the moment. Thanks for bringing this up! I'll consider your suggestion Commented Oct 24, 2014 at 20:06
  • There's little benefit to having an array of objects of arrays where each object has a single key that increments. You should just have an array of arrays, and grab the index you want or iterate all of them. var j = [["1","2","3"], ["4","5","6"], ["7","8","9"]]; Commented Oct 24, 2014 at 20:07

2 Answers 2

2

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
            }

    }
Sign up to request clarification or add additional context in comments.

2 Comments

@Liondancer: Aside from restructuring your data, this solution is more verbose than needed. There's no point to the for-in loop. It should just be if (str in j[i]) { /* do something with j[i][str] */ }
@RoyiNamir You helped me with my problem and provided a solution. However jfriend00 pointing out something and also provided a solution. I wish I can give you both check marks but I can't. I'm sorry but jfriend00 gave me a very helpful tip and an solution
1

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/

1 Comment

This helped a lot. Less iterations and simpler code. Thanks! =D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.