1

I am working on an array, Actually, I have a dropdown list. I am pushing that object into an array but I am facing one issue actually when I select something it shows two elements at the same time like in dropdown let suppose if I have options ( option1, option2, option3). For example, I have selected option1 it pushed object value as option1 but when I select option2 it also pushed it into an array. I want unique values like if I select options2 it should be select recent value.

 generateExtraFieldData = (data, name, index, type, value, values) => {
const { projectFloorData, unitModifiedData } = this.state
let obj = projectFloorData[name]
obj['data'] = data
let tempArr = [...unitModifiedData]

tempArr.push(obj)

this.setState({ unitModifiedData: this.uniqueFiles(tempArr) })

// projectFloorData[name].data = data

}

 uniqueFiles = (data) => {
    let unique = _.uniqBy(data, 'index')
    console.log('@@ array', unique)
    return unique
  }

I have used lodash but it just return ist selected value, I need a recent value like if the user selected a second time

1
  • 1
    Trying to be as respectful as possible: All your questions seem to be "I have an emergency", but it seems that you don't really have a great understanding the language or the tools you're using. You can't keep using "I'm a beginner" as an excuse to not understand the things you're meant to be working on. Sometimes you have to put the work in and learn. Commented Oct 21, 2021 at 7:19

2 Answers 2

1

Try using yourArray.length = 1;, this limit you array to an int limit. And if you select another option, just do yourArray.length = 0 to clear the array and then again yourArray.length = 1; to store the new option.

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

4 Comments

Can you please edit this code I am sorry I am beginner to JS
Could you please edit my code as I need it urgent.
is "tempArr" the array you put the objects on?
Yes.... it is ,
1

why are you pushing your selected value into your array if you want to select a single value at a time try an object instead of an array and change the value/ reassign the value on selection.

3 Comments

and if the problem is just it's inserting the same value then first try to filter the array with the key of the value and then check if length > 0 then don't push else push the object into the array;
Can you please edit my code as I am beginner. I need to reoslve this task . thanks
Can you please share the exact sample data

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.