简体   繁体   English

如何仅从对象数组中提取值并使用 react 和 javascript 将其放入数组中?

[英]How to extract only values from array of objects and put it to an array using react and javascript?

i have an array of objects like below我有一个对象数组,如下所示

const SelectedOptions = [
    {
        label: 'label1',
        value: '1',
    },
    {
        label: 'label2',
        value: '2',
    }
];

I use this selectedOption array in the onChange method of select menu like below我在 select 菜单的 onChange 方法中使用了这个 selectedOption 数组,如下所示

const App = () => {
    return (
        <Select
            options={options}
            onChange((selectedOptions) => {
                form.setFieldValue('optionsChosen', selectedOptions);
            });
        />
    );
}
         

Now the problem with above onChange method is that, i want to set field OptionsChosen to an array of values only.现在上面的 onChange 方法的问题是,我只想将字段 OptionsChosen 设置为一个值数组。

so in the onchange method i want to map through selectedOptions and extract only values field and put it to optionsChosen field.所以在 onchange 方法中,我想通过 selectedOptions 来 map 并仅提取值字段并将其放入 optionsChosen 字段。

so the expected array to optionsChosen field is like below,所以 optionsChosen 字段的预期数组如下所示,

["1", "2"];

how can i do it.我该怎么做。 could someone help me with this.有人可以帮我解决这个问题。 thanks.谢谢。

You need to use map to convert an array of objects into an array of values您需要使用map对象数组转换为值数组

 const SelectedOptions = [ { label: 'label1', value: '1', }, { label: 'label2', value: '2', } ]; result = SelectedOptions.map(option => option.value); console.log(result);

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

相关问题 如何将对象数组中的值放入JavaScript中的新数组中 - How to put values from an array of objects into a new array in JavaScript 从javascript中的对象数组中提取匹配值 - Extract matching values from array of objects in javascript 如何从嵌套在数组 JAVASCRIPT 内的对象中提取值 - how to extract values from an objects nested inside array JAVASCRIPT 如何遍历数组并使用 react 和 javascript 创建对象数组? - How to loop through an array an put create an array of objects using react and javascript? 如何从数组中提取对象以自动将它们放入变量中? - How to extract the objects from an array to put them in variables automatically? 如何从Javascript中的数组数组中提取值? - How to extract values from an array of arrays in Javascript? javascript从对象数组中提取键的相同值 - javascript extract same values of keys from array of objects Javascript:将对象值数组放入div - Javascript: Put array of objects values to divs 如何使用 JS Array map() 从 res.body.results 对象数组中提取月份值 - How to extract the month values from the res.body.results array of objects using JS Array map() 如何使用javascript从对象数组中提取对象的值 - How can I extract the value of an object from an array of objects array using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM