简体   繁体   English

如何在不更改原始数组的情况下更改对象Key

[英]How to change an object Key without mutating original array

I have an array of objects - I want to change one of the object keys to something else without mutating the original array. 我有一个对象数组-我想在不改变原始数组的情况下将对象键之一更改为其他对象。 what is the best method to approach this? 解决此问题的最佳方法是什么?

I understand that I could be using the Map method but unsure how this would work. 我了解我可以使用Map方法,但不确定如何使用。 thanks 谢谢

const books = [
  { title: "To Kill a Mockingbird", writtenBy: "Harper Lee" }, 
  { title: "A Clockwork Orange",  author: "Anthony Burgess" },
  { title: "The Elephant Tree", writtenBy: "R.D. Ronald" } 
]

function changeKey(arr, keyChange, newKey) {

}

// i want to return so the KEY keyChange(author) is changed to newKey(writtenBy)
[
 { title: "To Kill a Mockingbird", writtenBy: "Harper Lee" },
 { title: "A Clockwork Orange",  writtenBy: "Anthony Burgess" },
 { title: "The Elephant Tree", writtenBy: "R.D. Ronald" } 
]

You can map the parameter array and copy each of the objects within it shallowly using the spread operator. 您可以map参数数组,并使用散布运算符将其复制到其中的每个对象。 For each new object, if it contains the key we'd like to remove, copy the value to the new key and delete the old key. 对于每个新对象,如果它包含我们要删除的键,则将值复制到新键,然后delete旧键。

 const books = [ {title: "To Kill a Mockingbird", writtenBy: "Harper Lee"}, {title: "A Clockwork Orange", author: "Anthony Burgess"}, {title: "The Elephant Tree", writtenBy: "RD Ronald"} ]; const changeKey = (arr, keyChange, newKey) => arr.map(e => { const o = {...e}; if (keyChange in o) { o[newKey] = o[keyChange]; delete o[keyChange]; } return o; }) ; console.log(changeKey(books, "author", "writtenBy")); console.log(books); 

Array helpers like map, filter, reduce, etc doesn't mutate the original array they return a new array. 数组辅助对象(例如map,filter,reduce等)不会突变原始数组,而是返回新数组。 Map receives a function as an argument (callback). Map接收一个函数作为参数(回调)。 Map iterate the array applying your callback in every element. 映射迭代数组,在每个元素中应用回调。

 const books = [ {title: "To Kill a Mockingbird", writtenBy: "Harper Lee"}, {title: "A Clockwork Orange", author: "Anthony Burgess"}, {title: "The Elephant Tree", writtenBy: "RD Ronald"} ]; //Function to use as a callback on map function changeKey(current) { if(current.author) return { title: current.title, writtenBy: current.author }; return current; } //Creating new array applying changeKey in every element thanks to map const newBooks = books.map(changeKey); console.log(newBooks); 

The following will not mutate books array. 以下内容不会使books数组发生变化。

 const books = [ { title: "To Kill a Mockingbird", writtenBy: "Harper Lee" }, { title: "A Clockwork Orange", author: "Anthony Burgess" }, { title: "The Elephant Tree", writtenBy: "RD Ronald" } ]; const renamedBooks = books.map(book => { if (book.author) { return { title: book.title, writtenBy: book.author }; } return book; }); console.info(renamedBooks); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM