0

*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"
Sanya
  • 1,270
  • 5
  • 21
  • 47
  • If that JSON comes to you as a string first and you need to convert it to an `object` to make the Answers work, see https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object. – computercarguy Oct 23 '19 at 18:34

3 Answers3

3

You can use map instead of filter

const items = [
  {
    "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}
    ]
  }
]

const types = items.map(item => item.type)

console.log(types)

// [ 'Fruits', 'Vegetables', 'Meats' ]
Arnold Gandarillas
  • 3,896
  • 1
  • 30
  • 36
1

You should use map instead of filter:

var data = [
{
    "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}
    ]
}
]

console.log(data.map((obj) => obj.type))
ArrayKnight
  • 6,956
  • 4
  • 17
  • 20
1

Use the map function.

console.log(array.map(a => a.type));
pbachman
  • 934
  • 2
  • 11
  • 18