I have two objects in my react component as follows
cosnt [Selected, setSelected] = React.useState([]);
cosnt [NonSelected, setNonSelected] = React.useState([]);
const Tmp_Selected = [
{ "Metric": "AAA", "Weight": 10, "Value": "xxx" },
{ "Metric": "BBB", "Weight": 20, "Value": "xx1" },
{ "Metric": "CCC", "Weight": 30, "Value": "xx2" },
];
const Tmp_NonSelected = [
{ "Metric": "DDD", "Weight": 5, "Value": "yy" },
{ "Metric": "EEE", "Weight": 15, "Value": "zz" },
{ "Metric": "FFF", "Weight": 25, "Value": "cc" },
];
React.useEffect(() => {
setSelected(Tmp_Selected);
setNonSelected(Tmp_NonSelected);
}, [location]);
I have a function in my code where I will be getting only the Metric and Weight values of NonSelected object value based on that, I need to move that whole object from NonSelected object to Selected object
const MoveValues = (Metric='DDD', Weight=5) => {
}
So I need an something similar to this
const Tmp_Selected = [
{ "Metric": "AAA", "Weight": 10, "Value": "xxx" },
{ "Metric": "BBB", "Weight": 20, "Value": "xx1" },
{ "Metric": "CCC", "Weight": 30, "Value": "xx2" },
{ "Metric": "DDD", "Weight": 5, "Value": "yy" },
];
const Tmp_NonSelected = [
{ "Metric": "EEE", "Weight": 15, "Value": "zz" },
{ "Metric": "FFF", "Weight": 25, "Value": "cc" },
];
Any help is much appreciated
Thanks,
3
Answers
You can use the filter method of Arrays to remove object from the nonSelected Array.
Here is full code of your Component: