1

Go through the array numbers and put each number (2, 4, 5, 37, 0) as a property of the object numbers_duplicates. The value of each property should be that number multiplied by two. (first create the empty object before starting to add the properties and values).

var numbers= [2, 4, 5, 37, 0]
var duplicate_numbers = {} 

// later it should be like this { 0: 0, 2: 4, 4: 8, 5: 10, 37: 74}

enter code here for (var i = 0; i < numbers.length; ++i) {
duplicate_numbers.push( numbers[i]*2 );
    duplicate_numbers.push( i );}

How do I solve this problem?

1

6 Answers 6

2

Here you go mate!

    let arr = [2, 4, 5, 37, 0];
    let obj = {};

    arr.forEach((x) => {
       obj[x] = x*2
    });
    
    console.log(obj);

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

Comments

1

For get or set an object property, you can use bracket notation. Here is a version using the basic for loop you have written.

var numbers = [2, 4, 5, 37, 0];
var duplicate_numbers = {};

for (var i = 0; i < numbers.length; i++)
{
    duplicate_numbers[numbers[i]] = numbers[i] * 2;
}

console.log(duplicate_numbers);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Alternatively, you can also use Array.reduce() in this way:

var numbers = [2, 4, 5, 37, 0];

var duplicate_numbers = numbers.reduce((acc, n) => (acc[n] = 2 * n, acc), {});

console.log(duplicate_numbers);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Comments

0

You need to use a Object to do this, a object is made of key, value pairs.

const numbers_duplicates = {};
const numbers =  [2, 4, 5, 37, 0];

numbers.forEach(number => { numbers_duplicates[number] = number * 2 });

Comments

0
const numbers = [2, 4, 5, 37, 0] ;
const duplicateNumbers = {};
const numbersSorted = numbers.sort((a, b) => a - b);

for (let i = 0; i < numbersSorted.length; i++) {
  duplicateNumbers[numbersSorted[i]] = numbersSorted[i] * 2;
}

Comments

0

You may want to consider a Map. This is a special object exactly made for purposes that you need. It has a bunch of arraylike methods for handling that kind of objects. Please take a look Map.

var numbers= [2, 4, 5, 37, 0];
var duplicate_numbers = new Map();
numbers.forEach((val)=> {
    duplicate_numbers.set(val, val*2);
})

Comments

-1

var numbers = [2, 4, 5, 37, 0];
var duplicate_numbers = {};

var result = numbers.forEach(num => {
  duplicate_numbers[num] = num * 2
});

console.log(duplicate_numbers);

4 Comments

map will always return a new array. The OP wants an object.
updated the answer, please check now.. i miss understood the question
What if num is zero?
I think my answer is perfectly working accordin to the question, not sure why it's marked negative

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.