JavaScript Create new object from value of another object in js create object from array javascript,create object from class,create

What I want to do is really simple and there is a syntax error that I can't understand:

I have an array that contains some objects, and I want to create another array using some value of those objects:

let fields = [{type:'a',label:'email'},{type:'b',label:'name'},{type:'a',label:'tel'}]
let respone = []

// different implementation
fields.map(item => {
   respone.push({item.label: ''}) } )
`Uncaught SyntaxError: Unexpected token .`


fields.map(item => {
   respone = [...response, {item.label: ''}]  } )
`Uncaught SyntaxError: Unexpected token .`



fields.map(item => {
   respone = [...response, {item['label']: ''}]  } )
`Uncaught SyntaxError: Unexpected token [`

and obviously the error is from creating the object {item['label']: ''}. What is the reason for those errors, and what is the best way to create new objects from the existing object values.

Answer:1

So you have an array of objects, consisting of type and label. You want to make a "composite object" using that array. First thing, you don't want respone to be an array, you want it to be an object. Second, you want to name the key on respone to the value of item.label.

Try this code:

let fields = [{type:'a',label:'email'},{type:'b',label:'name'},{type:'a',label:'tel'}]

// Create an empty object
let respone = {}

// Iterate over the fields array, using each one to set the  key and value
//  on our respone object.
fields.forEach(item => { respone[item.label] = item.type} );

// Let's see how it looks!
console.log(respone);
Answer:2

Well the error is how you're trying to define key.

item.label this need to be changed to [item.label] as you want computed key (which needs to evaluated first and than used as key )

let fields = [{type:'a',label:'email'},{type:'b',label:'name'},{type:'a',label:'tel'}]

let respone = []

fields.forEach(item => {
   respone.push({[item.label]: ''}) } )
   
console.log(respone)
Answer:3

I have very little javascript skills and I would like to pass lines of a file as String arguments to a pre-written function. Basically what I want to do is read a file in this type of format, with ...

I have very little javascript skills and I would like to pass lines of a file as String arguments to a pre-written function. Basically what I want to do is read a file in this type of format, with ...

I'm in JavaScript for a while, but there is still soo much things to learn. Now, I had found some interesting facts about Switch statement. I know that when you use case someValue: without break; ...

I'm in JavaScript for a while, but there is still soo much things to learn. Now, I had found some interesting facts about Switch statement. I know that when you use case someValue: without break; ...

I'm trying to create a webview app with jquery that calculate GPA and I've created two buttons one of them for adding new elements and the other one to remove the elements and I'm trying to add more ...

I'm trying to create a webview app with jquery that calculate GPA and I've created two buttons one of them for adding new elements and the other one to remove the elements and I'm trying to add more ...

Consider this code. It is a gross simplification of the program I am writing. let numberOne; let numberTwo; function assignNumbers() { numberOne = Math.random()*10 numberTwo = Math.random()*10 } ...

Consider this code. It is a gross simplification of the program I am writing. let numberOne; let numberTwo; function assignNumbers() { numberOne = Math.random()*10 numberTwo = Math.random()*10 } ...