0

I am getting API response as:

[{"subject1": "English", "subject1": "Maths"}]

I want to store the values (English and Maths) into an array without keys like:

subject = ["English", "Maths"]
2
  • I think you mean [{"subject1":"English"}, {"subject1":"Maths"}]? Commented Oct 8, 2020 at 17:34
  • If the given JSON is correct, you will not be able to get both "English" and "maths" as you want due to them having the same key on the same object. Commented Oct 8, 2020 at 17:37

3 Answers 3

1

maybe this solve your problem:

// if you have variable number of subject
let res =  [{"subject1":"English", "subject2":"Maths"}]

let subjects = []

for(prop in res[0]){
  subjects.push(res[0][prop])
}
console.log(subjects)

Sign up to request clarification or add additional context in comments.

Comments

0

Does this solve your problem?

subject = [];
subject.push(Object.values(response));

Comments

0

Given an array of objects:

[{"subject1":"English"}, {"subject1":"Maths"}]

Use this:

let res = foo.map(e => e.subject1)
console.log(res) // prints ["English", "Maths"]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.