0

I have Declared options as

let options : any[] ;

and populated it using json response , I am using

<Select.Creatable
                                    ref="newSource_select"
                                    options={this.state.Options}
                                    id="newSource"
                                    value={this.state.newSource}
                                    onChange= {this.updateNewSource}                                       
                                    labelKey="label"
                                    valueKey="value"
                                    placeholder="Select New Source..."
                            />

 options =['value1' : 'value1' ,
          'value2' : 'value2'
          'value3' : 'value3' ]  // options values entered as mentioned ,

When i enter a value which in not in the options , it will provide a option to create a tag , tag is been created , but iam not able to view it in the options immediately , when the modal is closed and opened again , the entered option in been showed in the options of select . how can i resolve this

it is returning -1 ,

5
  • 5
    options =['value1' : 'value1' , 'value2' : 'value2' 'value3' : 'value3' ] this is not an array. Commented May 21, 2018 at 10:27
  • And you use index method not indexOf Commented May 21, 2018 at 10:27
  • 1
    Are you sure that is the exact code you used? You should be getting a syntax error well before you even get -1 returned Commented May 21, 2018 at 10:31
  • There are quite many misconceptions in this question. Please, fix the basic parts and update your question. Commented May 21, 2018 at 10:47
  • As you have mentioned above options =['value1' : 'value1' , 'value2' : 'value2' 'value3' : 'value3' ] it's not an array and you should have syntax error. First of all you missed coma after 'value2' and something like you want to achieve is Map not array. Map has key and value. If you want to have Array look at the comments, you have answer. If you want to have map go here: javascripture.com/Map Commented May 21, 2018 at 11:04

4 Answers 4

2

The data input you are using is neither object nor array.This below code might help you.

For array,

 var options =[ 'value1' ,
                  'value2',
                  'value3' ];
    var data = options.filter((val) => val.includes('value1'));

    console.log(data); 

If the data input is array of objects,then

items = [{id: 1, text: 'test words'}, {id: 2, text: 'another test'}];
var data = items.filter(item => item.text === 'test words')
console.log(data); 
Sign up to request clarification or add additional context in comments.

Comments

2

I don't think your array really is options =['value1' : 'value1' , 'value2' : 'value2' 'value3' : 'value3' ] because this cannot compile. You should be getting an error:

Uncaught SyntaxError: Unexpected token :

What I think you want to achieve here is an object? If so, here is how to do that:

options = {value1: 'value1', value2: 'value2', value3: 'value3'}

To then find if the value is present in the object, just do:

options.hasOwnProperty(value1)

Comments

2

you can use method include like this:

var array = ["a", "b", "c"];
array.includes("value2")

this method return true or false

1 Comment

I can confirm that this works in recent versions of React. This answer was helpful to me
0

If you have array:

var array = ["a", "b", "c"];

you can use method indexOf as you said:

array.indexOf("a");

And this method indexOf returns -1 when the value is missing in array.

So your array should looks like

options = ["value1", "value2", "value3"]

Comments

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.