0

It seems easy but it is not working for me. I want o add new array and name in object after fill input. When clicking at button OK should add new object. For example:

let arr = [{
    name: 'thomas',
    point: ''
  },
  {
    name: 'this should be value from input',
    point: ''
  }];

Now I have this code:

Name: <input type="text" id="input">
<button onclick="ok()">OK</button>

const input = document.querySelector('#input');

const arr = [{
    name: 'Thomas',
    point: ''
  }];
function ok(){


arr['name'].push(input.value);
console.log(input.value);
console.log(arr);
}

1 Answer 1

2

You need to create a new object, and callpush on the array, not the object inside it:

    arr.push(
// −^^^^^^^^
        {                         // This part is an "object
            name: input.value,    // literal" that creates
            point: ''             // a new object with these
        }                         // properties in it
    );
Sign up to request clarification or add additional context in comments.

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.