2

My main object looks like this:

const obj = {

    data: {
        'Puppies' : [],
        'Kittens': []
    }
};

I want to assign a value to the data field in data.Puppies[0].

When I try to do this using Object.assign() I get an error:

    Unexpected token, expected , (83:12)

  81 | 
  82 |     return Object.assign({}, obj, {
> 83 |         data.Puppies[0]: list
     |             ^
  84 |     });
  85 | }
  86 | 

I'm not sure how I can accomplish my task. I need to use Object.assign() because I need to return a new object not the original one. I am doing this because of a Redux reducer in ReactJS.

1
  • so basically cou need to get a copy from obj with an new item in Puppies at index zero without mutating the original object, right? Commented Feb 19, 2017 at 12:54

4 Answers 4

1

ES6 way without mutations:

const obj = {
    data: {
        'Puppies' : [],
        'Kittens': []
    }
};

const data = Object.assign({}, obj.data, {
                 Puppies: [...obj.data.Puppies, 'newValue']
             });    
const newObject = Object.assign({}, obj, {data});
console.log(newObject);

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

Comments

1

You could assign the outer object and assign to the array the new content at index 0 without mutating the original obj.

const list = ['foo', 'bar'];
const obj = { data: { Puppies: [], Kittens: [] } };

console.log(Object.assign({}, obj, { data: { Puppies: Object.assign([], obj.data.Puppies, { 0: list }) } }));
console.log(obj);

The same with content for Puppies.

const list = ['foo', 'bar'];
const obj = { data: { Puppies: ['bar', 42], Kittens: [] } };

console.log(Object.assign({}, obj, { data: { Puppies: Object.assign([], obj.data.Puppies, { 0: list }) } }));
console.log(obj);

4 Comments

What if puppies and kittens might change? How about a variable Kitten?
then you could use the pattern for Kitten as well { data: { Kitten: Object.assign([], obj.data.Kitten, { 0: data }) } }
NO I mean you have hard-coded Puppies and Kittens. What if we don't know which one it is?
if you dont know what array you need with some new data, you can not assign it, anyway. for a deep copy, you could use some other technique.
0

You could try using Ramdajs lens to achieve this

const obj = {
    data: {
        'Puppies' : [],
        'Kittens': []
    }
};

const list = [1,2,3,4];
const xlens = R.lensPath(['data', 'Puppies'])
console.log(R.set(xlens, list, obj))

Here is the code repel

2 Comments

😂,, you are obsessed by ramda .
nothing wrong but only editing answers of others and injecting this poor library
0

Replace Puppies with a new array with list as element 0 and the rest of Puppies starting with element 1, so effectively replacing element 0.

var obj = { data: { 'Puppies': [], 'Kittens': [] } };
var list = [ 'a', 'b' ];

var n = Object.assign({}, obj, {
  data: Object.assign({}, obj.data, {
    Puppies: [ list ].concat(obj.data.Puppies.slice(1))
  })
});

console.log(obj);
console.log(n);

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.