I have an API call that returns a JSON response object. The shape of the data is bit a confusing and I can't figure out the exact object to run .map() and .filter() on to get my desired result.
.then(response => {
console.log(response); //see below for shape of data
var dataSourceInfo = response.data.included.filter(
element => element.type === "DataSource"
);
var dataSourceName = dataSourceInfo.map(function(included) {
return included["name"];
});
console.log(dataSourceName);
In the two arrays, I'm attempting to filter over response.data.included to find an element by type. Then map over the returned filter to create a new ordered array. In one of the included arrays there is an identifying type of DataSource, here's an example:
included: [
{
id: "2147483604",
type: "DataSource",
name: "Some DataSource"
},
I'm logging the dataSourceName but the array only has the one of the expected names, and it's only from the first array, so it's like the map isn't reaching the second data.data. Any idea how I can get both names to appear in the filtered array?
Edit: correct response object is in the codesandbox
responseis an array soresponse.datashould throw an error. It should beresponse[0].dataorresponse.forEach/map/filter()typeelement ofDataSourcein one of theincludedarrays inresponse[0]andresponse[1]so the filter map should return[ dataSourceName1, dataSourceName2]dataproperty twice, so only one will survive (an object cannot have two properties with the same name).