-3

I have a Array

    var arr=[{t: "Lokanath", v: 0},
            {t: "Das", v: 1}]

Is there any way that i can get the Record t: "Das", v: 1 based on the value of v i.e. v:1

lokanath das
  • 736
  • 1
  • 10
  • 35

3 Answers3

0

Array.prototype.filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

You can use filter() in the following way:

var a=[{text: "Lokanath", value: 0, id: 100},
{text: "Das", value: 1, id: 101}];

var das = a.filter(p => p.text=='Das');
console.log(das[0]);

Please Note: Since filter() returns an array, you have to use index to take the object.

Mamun
  • 66,969
  • 9
  • 47
  • 59
  • 1
    Why use jQuery? This is native JS. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter – penguinflip Oct 17 '18 at 07:27
-1
var arr=[{text: "Lokanath", value: 0, id: 100},
            {text: "Das", value: 1, id: 101}]

arr.find(d => d.text == "Das")
Nitish Narang
  • 4,124
  • 2
  • 15
  • 22
  • While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Rohan Khude Oct 17 '18 at 07:49
-1

you can use filter method to do that.

var arr=[{text: "Lokanath", value: 0, id: 100},{text: "Das", value: 1, id: 101}];
console.log(arr.filter(o => o.text=='Das')[0]);
Negi Rox
  • 3,828
  • 1
  • 11
  • 18