2

I have an array object where there are key value pairs. I am trying to get the keys in that array using a loop but I am getting only 0. What is the problem with my code.

 var strj = '{"name":"John","age":"30","cars":
                                          [ {"type":"car", "year":"1998"}, 
                                            {"type":"van", "year":"1995"}]}';
    var myobj = JSON.parse(strj)

var care = myobj.cars.filter(c => c.type=='car');

Value of care

0:{type: "car", year: "1998"}
length:1
__proto__:Array(0)

Loop

for (var key in care){
    if(care.hasOwnProperty(key)){
        console.log(key)
    }
}
2
  • 2
    It's an array, you'll need to do care[0] otherwise the key will be the index, which is why the output is 0 Commented Jul 17, 2018 at 12:10
  • you did a filter and asked for the function to return only the type car which you only have one of. Value of care is as expected. Since care is a regular array, it's keys are indeed, 0,1,2,3,4.. Commented Jul 17, 2018 at 12:11

5 Answers 5

3

care is a array type so you cannot do for (var key in care). You need to do for (var key in care[0]). This is because for (var key in care) will look for the key value in care and since it is a array it will always take 0 as a value in key(as you have only one object in array and its index is 0). That is why you got 0 in console.log.

var care =[{type: "car", year: "1998"}];
for (var key in care[0]){
    if(care[0].hasOwnProperty(key)){
        console.log(key)
    }
}

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

1 Comment

Or if they do only want one object and not an array myobj.cars.find(c => c.type=='car') would be better
1
care.forEach( ( singleCar ) => {

    for ( var key in singleCar ){
        console.log(key);

        if( care.hasOwnProperty( key ) ){
            console.log(key);

        }
    }

})

forEach will give you all the objects one by one. so you can check them.

Comments

1

As others have solved the issue, might i make a suggestion - Object.keys () gives an array of the keys for a given object. Since you are getting your filtered object and simply want its keys - the following will achieve that. Note that this is only using the code after you have filtered the original and have gained the "care" object.

As an aside, note that object.values() will give you an array of the values in a given object and object.entries() will give you arrays of the key / value pairing.

var care = {type: "car", year: "1998"};
var keys = Object.keys(care)
console.log(keys) // gives ["type","year"]

Comments

1

filter() method returns a Array of matches.

var care = myobj.cars.filter(c => c.type=='car'); // So, this returns an array.

care.forEach(element => {
    console.log(Object.keys(element)); //Prints keys of each element
});

Comments

0

Well actually there is no problem in your code at all. But you just misunderstood the use of javascript filter. Javascript filter() creates new array that's why you are getting 0 as key. If you want to get only one matching element then find() is what you should use.

var strj = '{"name":"John","age":"30","cars":[{"type":"car", "year":"1998"},{"type":"van", "year":"1995"}]}';
var myobj = JSON.parse(strj)
var care = myobj.cars.filter(c => c.type == 'car'); // returns array
var care = myobj.cars.find(c => c.type == 'car'); // returns first matching object
var care = myobj.cars.findIndex(c => c.type == 'car'); // returns first matching index
  1. Javascript filter() method => Read Here
  2. Javascript find() => Read Here
  3. Javascript findIndex() method => Read Here

Comments

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.