0

I have an array like this:

language: 
 [
   {added: "English"}
 ]

What I want to do is to remove the key added but I want to keep the value English in the same array.

The result I except:

language: 
 [
  "English"
 ]

By far I have tried something like this:

for(let i  in language) {
 delete language[i].added
}
console.log(language)

This will remove the key and the value as well. How can I remove the key, but keep the value in the same array?

1

2 Answers 2

2

If the objects just consist of a single property, added, you can use Array.map to convert the objects into scalar values:

data = {
  language: [
    { added: "English" }
  ]
}

data.language = data.language.map(o => o.added)

console.log(data)

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

Comments

2

You aren't deleting a key, you're replacing an entry in an array. The old entry is an object with a single key and the new one is it's value:

 language = [
   {added: "English"}
 ]
 
for (let i in language) {
  language[i] = Object.values(language[i])[0]
}
 
 console.log(language);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.