0

I have an array of objects:

var subcategories = [{name:'gloves', tag:'wool'}, {name:'boots', tag: 'leather'}]

All I want to do is to find the index of the object to change a name or a tag. I use this function:

function setSubcat(val, index, lang){
   var newArr = []
   var obj = {
      'name': val
   }
   subcategories.map((val, i)=>{
      if(index === i){
         var newObj = Object.assign(val, obj)
         newArr.push(newObj)
      }
      newArr.push(val)
   })
   setSubcategories(newArr)
}
           

The error happens at var newObj = Object.assign(val, obj)

I thought the error means I can't mutate the state directly and so I have to make a copy. I thought that mapping through subcategories and push it into a local newArr means I made a copy of the array. But it wasn't working when I wanted to change a value of object in it so I used Object.assign which I thought would deep copy the particular object from the array, but it's not working either.

What am I missing here?

5
  • 1
    Not reproducible: typescriptlang.org/play?#code/… Commented Sep 22, 2020 at 12:27
  • agreed; could you post more code or a playground example? this code is fine as is. Commented Sep 22, 2020 at 12:28
  • 1
    Please don't use map that way. Everyone is going to read that code and think "Is that a bug? I need to ask him....". map is for transforming each element, forEach is for visiting each element. Commented Sep 22, 2020 at 12:29
  • 1
    Still working with map used correctly: typescriptlang.org/play?#code/… Commented Sep 22, 2020 at 12:33
  • @Lesiak this is it. Thanks a bunch Commented Sep 22, 2020 at 12:41

1 Answer 1

1

As pointed in comments:

  • the code you posted does not show how you created object with unmodifiable property
  • you can create a new object and not use Object.assign to get rid of the error
  • use map function in more idiomatic way
interface TaggedItem {
    name: string,
    tag: string
}
var subcategories = [{name:'gloves', tag:'wool'}, {name:'boots', tag: 'leather'}];

function setSubcat(val: string, index: number, _lang: any){
   var obj: Partial<TaggedItem> = {
      'name': val
   }
   var newArr: TaggedItem[] = subcategories.map((val, i)=>{
      if(index === i){
        return {
            ...val,
            ...obj
        } 
      } else {
        return {...val};
        // or return val; if you don't need a full copy
      }
   })
   console.log(newArr);
}

setSubcat('newName', 0, undefined);

Playground link

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

1 Comment

Yeah the way I was using map didn't help. This is really neat, thanks again.

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.