Search code examples
arraysjsonnode.jsapache-sparkgulp

Multiple, mostly empty ID arrays returned


I'm new to node.js and admittedly I'm probably trying to learn the hard way but I use the following code to parse json looking for the id when the corresponding name contains the uuid. The uuid gets passed from a different function.

var arrFound = Object.values(json).filter(function(obj) {
    return obj.id && obj.name.includes(uuid);
}).map(function(obj) {
    return obj.id;
});
console.log (arrFound); // array of matched id values

I expect this to return something like [ 'local-1517085058363' ].

Instead I get this:

[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]

The json comes from the Spark HistoryServer API(http://127.0.0.1:18080/api/v1/applications), although it's just JSON so shouldn't matter where it comes from. Here is a sample:

[{
  "id" : "local-1517085058363",
  "name" : "TeraGen (5MB) 6e722700-0397-11e8-ae84-b54a3ebb27aa",
  "attempts" : [ {
    "startTime" : "2018-01-27T19:22:37.513GMT",
    "endTime" : "2018-01-27T19:22:43.253GMT",
    "lastUpdated" : "2018-01-27T19:22:43.000GMT",
    "duration" : 5740,
    "sparkUser" : "paulcarron",
    "completed" : true,
    "endTimeEpoch" : 1517080963253,
    "startTimeEpoch" : 1517080957513,
    "lastUpdatedEpoch" : 1517080963000
  } ]
} ] 

Also, I'm running my code on Gulp. I'm not sure if that will make any difference.

How do I fix this so I only return one single id?


Solution

  • There was multiple arrays so that explains where they came from. I used a reduce() to get the object I was looking for.