[英]How can I destructure an array within my object in React ES6?
I'm currently developing a React app where an Application will be selected, and the waterTypes for this application should then be returned.我目前正在开发一个 React 应用程序,其中将选择一个应用程序,然后应该返回该应用程序的 waterTypes。
I therefore need to destructure the waterTypes array within my filtered ApplicationData object.因此,我需要在过滤后的 ApplicationData object 中解构 waterTypes 数组。 The array will have either 1, 2, or 3 values present.该数组将包含 1、2 或 3 个值。
Currently my waterTypes variable is returning undefined to the console.目前我的 waterTypes 变量正在返回 undefined 到控制台。 How can I get this to return the waterTypes from the selected Application?我怎样才能让它从选定的应用程序中返回 waterTypes?
ChoicesProvider.js ChoicesProvider.js
import React, { createContext, useState } from "react";
export const ChoicesContext = createContext(null);
export const ChoicesProvider = ({ children }) => {
const [choices, setChoices] = useState({
category: null,
});
return (
<ChoicesContext.Provider value={{ choices, setChoices }}>
{children}
</ChoicesContext.Provider>
);
};
ApplicationData.js应用程序数据.js
const ApplicationData = [
{
name: 'App1',
waterTypes: ['Type 1']
},
{
name: 'App2',
waterTypes: ['Type 1', 'Type 2']
},
{
name: 'App3',
waterTypes: ['Type 1', 'Type 2', 'Type 3']
},
];
export default ApplicationData
Product.js产品.js
const Product = () => {
const { choices, setChoices } = useContext(ChoicesContext);
const CurrentApplication = ApplicationData.filter(x => x.name === choices.category);
const { waterTypes } = CurrentApplication;
console.log(waterTypes);
return (
<>
</>
);
};
export default Product
Use find
instead of filter
, to destructure an object not an array of one item使用find
而不是filter
来解构 object 而不是一个项目的数组
const ApplicationData = [ { name: 'App1', waterTypes: ['Type 1'] }, { name: 'App2', waterTypes: ['Type 1', 'Type 2'] }, { name: 'App3', waterTypes: ['Type 1', 'Type 2', 'Type 3'] }, ]; const CurrentApplication = ApplicationData.find(x => x.name === 'App3'); const { waterTypes } = CurrentApplication; console.log(waterTypes)
Array.filter always returns an array Array.filter 总是返回一个数组
const Product = () => {
const { choices, setChoices } = useContext(ChoicesContext);
const CurrentApplication = ApplicationData.filter(x => x.name === choices.category);
const { waterTypes } = CurrentApplication[0];
console.log(waterTypes);
return (
<>
</>
); ); }; };
Array.prototype.filter
will always return an array, if you want to use filter you have loop over the filteredArray
to get the value Array.prototype.filter
将始终返回一个数组,如果您想使用过滤器,您可以遍历filteredArray
器数组来获取值
const applicationData = [ { name: 'App1', waterTypes: ['Type 1'] }, { name: 'App2', waterTypes: ['Type 1', 'Type 2'] }, { name: 'App3', waterTypes: ['Type 1', 'Type 2', 'Type 3'] }, ]; const filteredArray = applicationData.filter(data => data.name === 'App3'); for (let item of filteredArray) { const { waterTypes } = item; console.log(waterTypes); }
Also you can use Array.prototype.find
is you only want to find one item in the array based of a condition.您也可以使用Array.prototype.find
是您只想根据条件在数组中查找一项。
For example,例如,
const applicationData = [ { name: 'App1', waterTypes: ['Type 1'] }, { name: 'App2', waterTypes: ['Type 1', 'Type 2'] }, { name: 'App3', waterTypes: ['Type 1', 'Type 2', 'Type 3'] }, ]; const { waterTypes } = applicationData.find(data => data.name === 'App3'); console.log(waterTypes)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.