-2

I am new to JSON and Javascript. I have JSON object as below:

const text = {"recordset": [
    {
        "planned_date": "2020-06-29T00:00:00.000Z",
        "quantity": 9,
    },
    {
        "planned_date": "2020-07-06T00:00:00.000Z",
        "quantity": 12,
    },
    {
        "planned_date": "2020-07-13T00:00:00.000Z",
        "quantity": 13,
    },
    {
        "planned_date": "2020-07-20T00:00:00.000Z",
        "quantity": 9,
    }
 
]};

I tried to create a new array which only take the "quantity" as such but failed miserably:

for (i in text.recordset) {
    for (j in text.recordset[i].quantity) {
        newText += text.recordset[i].quantity[j]
    }
}

What I want is new array as below:

const newText = [9, 12, 13, 9]
rain123
  • 243
  • 4
  • 13

2 Answers2

3

You can make use of .map method in js array.

const text = {"recordset": [
    {
        "planned_date": "2020-06-29T00:00:00.000Z",
        "quantity": 9,
    },
    {
        "planned_date": "2020-07-06T00:00:00.000Z",
        "quantity": 12,
    },
    {
        "planned_date": "2020-07-13T00:00:00.000Z",
        "quantity": 13,
    },
    {
        "planned_date": "2020-07-20T00:00:00.000Z",
        "quantity": 9,
    }
 
]};

var result = text.recordset.map(obj => obj.quantity);
console.log(result);
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
1

text.recordset.map(r => r.quantity)