JavaScript Cannot get/fetch keys from an array in javascript cannot open .get/fetch_head,apt-get cannot fetch,fetch cannot

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)
    }
}
Answer:1

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)
    }
}
Answer:2
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.

Answer:3

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"]
Answer:4

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
});
Answer:5

I'm moving a piece of our programming code from client side to server side because of performance issues (note: not all code is in the post, just the part i'm having trouble with) the specific piece ...

I'm moving a piece of our programming code from client side to server side because of performance issues (note: not all code is in the post, just the part i'm having trouble with) the specific piece ...

I find the functionality of Array.prototype.join very useful because it only applies the join value to the "inner" connections of the elements in the array. Like so: ['Hey', 'there'].join('-') // Hey-...

I find the functionality of Array.prototype.join very useful because it only applies the join value to the "inner" connections of the elements in the array. Like so: ['Hey', 'there'].join('-') // Hey-...

  1. flatmap works

I am using Bootstrap tabs to load a widget from a server. What I need is to load the tab content as soon as the user click the tab instead of the initial load of the page. For ex below is the HTML for ...

I am using Bootstrap tabs to load a widget from a server. What I need is to load the tab content as soon as the user click the tab instead of the initial load of the page. For ex below is the HTML for ...

  1. bootstrap load content
  2. bootstrap load content on scroll
  3. bootstrap load content on tab click
  4. bootstrap load content on click
  5. bootstrap load content in modal
  6. bootstrap load div
  7. bootstrap load tab content ajax
  8. bootstrap load more content
  9. bootstrap load dynamic content
  10. bootstrap load content into div
  11. load a bootstrap popover content with ajax

Why I am getting the result of -'54'+30 expresion as -24 however when I tried to remove the (minus)- from expression then It is just concatenation of string and gives 5430 as output.Can anyone explain ...

Why I am getting the result of -'54'+30 expresion as -24 however when I tried to remove the (minus)- from expression then It is just concatenation of string and gives 5430 as output.Can anyone explain ...

  1. javascript result object
  2. javascriptresult function
  3. javascript result in mvc
  4. javascript result type
  5. javascript result nan
  6. javascript result keyword
  7. javascript result in html
  8. javascript result
  9. javascript result.push
  10. javascript result undefined
  11. javascript result of assignment
  12. javascript result in asp.net mvc
  13. javascript result of promise
  14. javascript result array
  15. javascript result is not defined
  16. javascript result c#
  17. javascript result disappears
  18. javascript result variable
  19. javascript result to json
  20. javascript result.length