'How to add key values pair in object in JAVASCRIPT?

how can I push keys and values in an empty object?

You are provided with an array, possibleIterable. Using a for loop, build out the object divByThree so that each key is an element of possibleIterable that is divisible by three. The value of each key should be the array index at which that key can be found in possibleIterable.

const possibleIterable = [4, 3, 9, 6, 23];
const divByThree = {};
// ADD CODE HERE
for (let i =0 ; i < possibleIterable.length; i++)
  {
    if (possibleIterable[i] % 3 === 0)
   {
      // ADD CODE HERE!
         
      }
  }
console.log(divByThree)


Solution 1:[1]

for (let i = 0; i < possibleIterable.length; i++) {
  if (possibleIterable[i] % 3 === 0) {
    divByThree[possibleIterable[i]] = i;
  }
}

This is probably what you were looking for. This assigns new key-value pairs, where the key is an element in possibleIterable that is divisible by 3 and the value is the index position of that element.

So if you console.log(divByThree), it should print out

{ 3: 1, 6: 3, 9: 2 }

I understand that this is an old post, but thought I should give an answer for anyone looking in the future.

Solution 2:[2]

divByThree.key = value

or

divByThree['key'] = value

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Syscall
Solution 2 Eriks Klotins