0
defaultParameterTypes = [] // this is my empty array



const propsArray = this.props.device.deviceProperties.defaultParameterTypes;      
 const defaultParameterTypes = this.state.deviceSettings.defaultParameterTypes;

when i checked on checkbox i want to push into my defaultParameterTypes array the object but the case is when the type is repeated dont push it twice.

"propsArray": "[{'name': '1','type': '11'}, {'name': '2','type': '22'}, {'name': '3','type': '11'}, {'name': '4','type': '11'}, {'name': '5','type': '22'}, {'name': '6','type': '22'}, {'name': '7','type': '22'}]",

i want to make like loop and check if it is not found in defaultEmptyArray and push it if not found but i dont need the type to be repeated in mydefaultarray

7
  • 2
    So have you made the loop to check to push elements or not? Update your question with your attempt and where you are getting stuck. Commented Jan 31, 2020 at 9:07
  • i dont know how to make it Commented Jan 31, 2020 at 9:08
  • for (let i = 0; i < propsArray.length; i++) { if (e.target.checked) { if (propsArray[i].type === defaultParameterTypes[j].type) { alert("Dont Push this type") // break; } else { alert("Push this type"); defaultParameterTypes.push(propsArray[i]); // break; } }} Commented Jan 31, 2020 at 9:10
  • i was trying like this but i need to make like to check if it is found in defaultParameterTypes or not Commented Jan 31, 2020 at 9:11
  • What is your expected result? If type seen again, don't copy, or only maintain one copy overall, i.e. overwrite it each time so the last one seen is what remains? Commented Jan 31, 2020 at 9:12

2 Answers 2

1

Simple way would be to create a map of the types seen in the array and reduce your input array into it, then get the array of values from the map you created.

const data = [
  { name: "1", type: "11" },
  { name: "2", type: "22" },
  { name: "3", type: "11" },
  { name: "4", type: "11" },
  { name: "5", type: "22" },
  { name: "6", type: "22" },
  { name: "7", type: "22" }
];

// reduce array into map of type => { name, type }
// then get the object values array
const reducedData = Object.values(
  data.reduce((acc, { name, type}) => {
    if (!acc[type]) acc[type] = { name, type }; // if not seen type, store
    return acc;
  }, {})
);

console.log(reducedData)

Expand on this concept to create a function that takes two arrays and processes the second into the first.

const data = [
  { name: "1", type: "11" },
  { name: "2", type: "22" },
  { name: "3", type: "44" },
  { name: "4", type: "11" },
  { name: "5", type: "22" },
  { name: "6", type: "33" },
  { name: "7", type: "22" }
];

const data2 = [
  { name: "1", type: "33" },
  { name: "2", type: "22" },
  { name: "3", type: "66" },
  { name: "4", type: "11" },
  { name: "5", type: "22" },
  { name: "6", type: "44" },
  { name: "7", type: "22" }
];

const data3 = [
  { name: "1", type: "66" },
  { name: "2", type: "22" },
  { name: "3", type: "33" },
  { name: "4", type: "11" },
  { name: "5", type: "55" },
  { name: "6", type: "11" },
  { name: "7", type: "44" }
];

const reduceData = (currentArray = [], newArray = []) => {
  const mapFn = (acc, { name, type }) => {
    if (!acc[type]) acc[type] = { name, type }; // if not seen type, store
    return acc;
  };
  const createMap = array => array.reduce(mapFn, {});

  return Object.values(newArray.reduce(mapFn, createMap(currentArray)));
};

const reducedData = reduceData(data, data2);
const reducedData1 = reduceData(reducedData, data3);

console.log(reducedData);
console.log(reducedData1);

Sign up to request clarification or add additional context in comments.

12 Comments

but i need one more thing in same issue
i want to compare the 2 arrays the default and the props array
( i want to compare only the type of the object) that if first item in propsArray is not fount in defaultParameterTypes array then push the object to defaultParameterTypes
Thanks alot Mr. Drew
But the first is better
|
0
let hasValue = (arr, obj) => arr && arr.filter(item => item.type == obj.type).length

let result = propsArray.reduce((acc, curr) => {
 if (hasValue(acc, curr)) {
  return acc;
 } else {
  return [...acc, curr]
 }
}, []);

will give u an array with all the elements where the type property is unique..

hope this is what ur expected result..

let result = propsArray.reduce((acc, curr) => {
 if (hasValue(acc, curr)) {
  return [...acc, { name: curr.name }];
 } else {
  return [...acc, curr]
 }
}, []);

or if type reccurs, copy only the name object..

this will return an array of same length but along the elements, type property will be removed if it recurrs...

8 Comments

if you please send me your email to contact you and send you the screenshot and what i am facing
and really thank you very much you helped me to think more when you send your examples
if (checked) { defaultParameterTypes.push({ "name": e.target.name, "type": e.target.value }) this.setState({ deviceSettings: { ...this.state.deviceSettings, defaultParameterTypes } }); }
else { let defaultParameterTypes = this.state.deviceSettings.defaultParameterTypes; const i = defaultParameterTypes.findIndex( item => item.type === e.target.value ); defaultParameterTypes = defaultParameterTypes.slice(0, i).concat( defaultParameterTypes.slice(i + 1, defaultParameterTypes.length) );
this.setState({ deviceSettings: { ...this.state.deviceSettings, defaultParameterTypes } }) }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.