0

Here is my code:

const array1 = [{a: 'abc', b: 'cd'}, {a: 'abc', b: 'xyz'}, {a: 'abc', 
b: 'mno'}];
let obj = array1.reduce(function(result, item, index){
    result[index] = item
  return result;
}, {});

let dealId = 123;
let value = {};
let array2 = [];
for (var property in obj) {
   value[dealId] = array2.push(obj[property]);
}
console.log(value)

The output of this is Object { 123: 3 }

But I want and this is what I was expecting. Object { 123: [{a: 'abc', b: 'cd'}, {a: 'abc', b: 'xyz'}, {a: 'abc', b: 'mno'}] }

Why am I getting 3 instead of an array? How to get the array?

1
  • 1
    Push returns the new length of the array. Move the assignment outside the loop. Also, you can simply use Object.values(obj) instead (with a difference that inherited properties won't be listed). Commented Aug 29, 2018 at 8:18

5 Answers 5

1

Array.prototype.push returns the new length of the array, not the array itself. Your loop assigns the values 1, 2 and 3 to the same value[dealId].

Instead, you can move the assignment outside the loop:

for (var property in obj) {
   array2.push(obj[property]);
}
value[dealId] = array2;

Or you can simply use Object.values:

value[dealId] = Object.values(obj);

But note that this does not list inherited properties.

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

Comments

1

Why not build a new object with a single key and the given array as value?

For the object use a computed property name.

const
    array = [{ a: 'abc', b: 'cd' }, { a: 'abc', b: 'xyz' }, { a: 'abc', b: 'mno' }],
    key = 123,
    object = { [key]: array };

console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

3 Comments

but his expected key is without "". Expected is 123 not "123"
@VicJordan, the keys of objects are always strings.
Yes I know but I think it would be better if we can tell the same to OP i.e. this information in your question. Thanks!
0

you need to add into aaray first then create the object

 

const array1 = [{a: 'abc', b: 'cd'}, {a: 'abc', b: 'xyz'}, {a: 'abc', b: 'mno'}];
let obj = array1.reduce(function(result, item, index){
    result[index] = item
  return result;
}, {});

let dealId = 123;
let value = {};
let array2 = [];
for (var property in obj) {
  array2.push(obj[property]);
}
 value[dealId] = array2;
console.log(value);

Comments

0

const array1 = [{a: 'abc', b: 'cd'}, {a: 'abc', b: 'xyz'}, {a: 'abc', 
b: 'mno'}];
let obj = array1.reduce(function(result, item, index){
    result[index] = item
  return result;
}, {});

let dealId = 123;
let value = {};
let array2 = [];
for (var property in obj) {
   array2.push(obj[property]);
}

value[dealId] = array2;

console.log(value)

Comments

0

Assign your new obj property (id) the requested array:

    let arr = [{a: 'abc', b: 'cd'}, {a: 'abc', b: 'xyz'}, {a: 'abc', 
    b: 'mno'}];
    let newObj = {};
    let id = 123;

    newObj[id] = arr;
    console.log(newObj);

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.