简体   繁体   English

JavaScript:如何更改数组中对象的属性名称?

[英]JavaScript: How can I change property names of objects in an array?

I am using this react-select : https://github.com/JedWatson/react-select我正在使用这个react-selecthttps : //github.com/JedWatson/react-select

The format for options data that they require is:他们需要的选项数据的格式是:

const options = [
    { value: 'chocolate', label: 'Chocolate' },
    { value: 'strawberry', label: 'Strawberry'},
    { value: 'vanilla', label: 'Vanilla' }
];

My array is set up differently as follows:我的阵列设置不同,如下所示:

const columns = [
    { name: 'OrderNumber', title: 'Order Number' },
    { name: 'strawberry', title: 'Strawberry' },
    { name: 'vanilla', title: 'Vanilla' }
]

I am not able to change my array.我无法更改我的数组。 If try to use name or value in my option items, I encounter issues using them with select-react .如果尝试在我的选项中使用namevalue ,我会遇到在select-react使用它们的问题。 If I change my name to value , the select options are populating, however I don't want to do that.如果我将name更改为value ,则会填充选择选项,但是我不想这样做。

Can anyone teach me how can I change my array's name to value ?谁能教我如何将数组的name更改为value

You could use the .map() function to make the data in columns suitable for use with react-select .您可以使用.map()函数使columns的数据适合与react-select

The .map() function is available on the Array type. .map()函数可用于Array类型。 It creates a new array from the array you call it on, and allows you to provide a function that transforms/changes each item as it is copied from the original array.它从您调用它的数组中创建一个新数组,并允许您提供一个函数来转换/更改从原始数组复制的每个项目。

You can make use of it as follows:您可以按如下方式使用它:

const columns = [
    { name: 'OrderNumber', title: 'Order Number' },
    { name: 'strawberry', title: 'Strawberry' },
    { name: 'vanilla', title: 'Vanilla' }
]

const options = columns.map(function(row) {

   // This function defines the "mapping behaviour". name and title 
   // data from each "row" from your columns array is mapped to a 
   // corresponding item in the new "options" array

   return { value : row.name, label : row.title }
})

/*
options will now contain this:
[
    { value: 'OrderNumber', label: 'Order Number' },
    { value: 'strawberry', label: 'Strawberry' },
    { value: 'vanilla', label: 'Vanilla' }
];
*/

For more information, see the MDN documentation for .map()有关更多信息, 请参阅.map()的 MDN 文档

If you just want to rename the name property to value you can use a map and destruct the name property as value and pick the rest.如果您只想将name属性重命名为value您可以使用map并将name属性破坏为value并选择其余的。

 const columns = [ { name: 'OrderNumber', title: 'Order Number' }, { name: 'strawberry', title: 'Strawberry' }, { name: 'vanilla', title: 'Vanilla' } ]; const newColumns = columns.map( item => { const { name: value, ...rest } = item; return { value, ...rest } } ); console.log( newColumns );

But, I suspect that you would want this since react-select doesn't work (as far as I see) with title .但是,我怀疑您会想要这个,因为react-selecttitle不起作用(据我所知)。 It waits for a label prop I guess.我猜它在等待label道具。 If this is so, go with and change all the properties as @Dacre Denny suggested.如果是这样,请按照@Dacre Denny 的建议更改所有属性。 I like arrow functions :) So:我喜欢箭头函数 :) 所以:

const newColumns = columns.map( item =>
  ( { value: item.name, label: item.title } )
);

Use destructuring with renaming property will simplify.使用具有重命名属性的destructuring将简化。

 const options = [ { value: "chocolate", label: "Chocolate" }, { value: "strawberry", label: "Strawberry" }, { value: "vanilla", label: "Vanilla" }, ]; const columns = options.map(({ value: name, label: title }) => ({ name, title, })); console.log(columns);

暂无
暂无

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

相关问题 如何映射对象数组的名称以更改字段名称? - How can I map the names of an array of objects to change the field names? 如何更改 Javascript 中对象数组中的属性名称? - How to change property name in array of objects in Javascript? 如何从包含嵌套对象的JavaScript对象文字中获取所有属性名称? - How can I get all the property names from a javascript object literal that contains nested objects? 如何将javascript动态数组转换为具有自定义键名的多个对象的数组 - How can i convert a javascript dynamic array to an array of multiple objects with custom key names 如何定位数组中的对象子集并将属性更改为每个对象的递增数字? - How can I target subset of objects in array and change a property to an incrementing number for each of those objects? 如何在React Native中更改对象属性的JavaScript数组 - How to change JavaScript array of objects' property in react native 我可以更改数组内对象属性的 state 吗? - can i change the state of property of objects inside an array? 如何获取数组中对象的属性名称? - How to get property names of objects in array? 如何更改对象数组中的属性 - How To Change A Property In An Array Of Objects 如何更改对象属性IF的值,并且仅在该值满足特定条件的情况下才能更改? (JavaScript) - How can I change the value of an objects property IF and only IF that value meets a certain condition? (Javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM