*NOTE: I'm looking for a solution in Vanilla JS (no jQuery) and preferably not a for-loop.
I have a simple JSON dataset where I want to push each "type" into an array of strings.
[
{
"type": "Fruits",
"objects":
[
{"name": "Apples", "qty":35},
{"name": "Bananas", "qty":48},
{"name": "Oranges", "qty":12}
]
},
{
"type": "Vegetables",
"objects":
[
{"name": "Celery", "qty":255},
{"name": "Potatos", "qty":105},
{"name": "Carrots", "qty":483},
{"name": "Peas", "qty":350}
]
},
{
"type": "Meats",
"objects":
[
{"name": "Lamb", "qty":255},
{"name": "Chicken", "qty":545},
{"name": "Beef", "qty":13}
]
}
]
The output should be:
["Fruits", "Vegetables", "Meats"]
I parsed the JSON object into a variable (that works) but
I don't understand why the simple filter
function won't work:
var myData = require("../data.json");
console.log(myData); // <-- Yay! I have my data
//Retrieve all "type" strings into an array (** this isn't working **)
var myTypes = myData.filter(a => a.type);
console.log(myTypes); // <-- This shows an array of objects (like above) and not an array of string "type"