1

Here I have one month object In this month object inside value 1,2,3,4,5 and selected experience value is 04 (see console) so want to find value and I compare both but I got undefined because 4 and 04 not matched How to match them ?

console.log(selectedExperience.from.split('/')[0])) // 04

console.log(months) // [ { value : 1, name: "one"}
                          { value: 2, name: "two" }
                          { value: 4, name: "four" } ]

console.log(months.find(month => month.value === selectedExperience.from.split('/')[0])); // undefined
4
  • 1
    Your best bet would be to use parseInt on your "04" string. Such as parseInt(selExp.from.split("/")[0], 10) which will yield the number 4 Commented Mar 18, 2019 at 16:06
  • The split method return a String and because of your comparison "===" with the value which is a INT, your find does not return anything Commented Mar 18, 2019 at 16:10
  • @DDD can you please stop asking people to upvote your question? I'm not sure it's entirely cool, and it's definitely annoying. If people want to upvote your question they will. Just roll with it. Commented Mar 18, 2019 at 17:25
  • @Pierce ok thank you you r right Commented Mar 19, 2019 at 4:36

6 Answers 6

3

What about to convert it into an int?

console.log(months.find(month => month.value === parseInt(selectedExperience.from.split('/')[0])));
Sign up to request clarification or add additional context in comments.

1 Comment

my bad ! i wanted to wrote it on the question !
2

You can use parseInt

month.value === parseInt(selectedExperience.from.split('/')[0]) 

console.log(parseInt('04') === 4)

Comments

2

Use Number so you are comparing numbers not strings:

Number(month.value) === Number(selectedExperience.from.split('/')[0])

Comments

2

try to convert string to number with "+" operator:

var test = "04";
console.log(test); //04
console.log(+test); //4

console.log(months.find(month => month.value === +selectedExperience.from.split('/')[0]));

Comments

1

Assuming 04 is a string , do a parseInt or convert it to number before comparing or else use unary operator

console.log(selectedExperience.from.split('/')[0])) // 04
let exp = parseInt(selectedExperience.from.split('/')[0],10)

console.log(months)  // [ { value : 1, name: "one"}
                     //   { value: 2, name: "two" }
                     //   { value: 4, name: "four" }] 

console.log(months.find(month => month.value ===exp  ))

Comments

1

.split() will return you an array of strings. So for comparing with an integer, you need to parse it using parseInt.

var test = "04/12";
var months = [{
    value: 1,
    name: "one"
  },
  {
    value: 2,
    name: "two"
  },
  {
    value: 4,
    name: "four"
  }
];

console.log(test.split('/')[0]);
console.log(months);

console.log(months.find(month => month.value === parseInt(test.split('/')[0])));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.