Javascript get value based on another in object array

I am trying to return a value from an array of objects in javascript, the first object in the array:

 dict=[{index:"1",caption:"AAAffterA",blurb:"stuff to write here asieh 1flsidg"}]

      

how can I return the "ad" value (B) if I have the "caption" value (A)? I managed to do it in a looong way, but I'm sure there is an easier way?

A = "AAAffterA"
var result = dict.map(function(a) {return a.caption;});
key = jQuery.inArray(A,result)
B = dict[key].blurb

      

+3


source to share


4 answers


Only one function to call.

var dict = [{ index: "1", caption: "AAAffterA", blurb: "stuff to write here asieh 1flsidg" }],
    a = "AAAffterA",
    b = dict.reduce(function (res, el) {
        return el.caption === a ? el.blurb : res;
    }, undefined);
alert(b);
      

Run codeHide result




Here's a solution for multiple occurrences:

var dict = [{ index: "1", caption: "AAAffterA", blurb: "stuff to write here asieh 1flsidg" }, { index: "2", caption: "AAAffterA", blurb: "index 2 stuff to write here asieh 1flsidg" }],
    a = "AAAffterA",
    b = dict.reduce(function (res, el) {
        el.caption === a && res.push(el.blurb);
        return res;
    }, []);
alert(JSON.stringify(b));
      

Run codeHide result


+1


source


You almost had it.

Instead of returning caption

and then looking for it in again dict

, just return it blurb

back from the map. It will return something like this ["stuff to write here asieh 1flsidg"]

Now you can shrink the last two lines by simply extracting the first result directly from the returned array. The first element of the newly created array is the string you want to return. ["stuff to write here asieh 1flsidg"][0]

- it's simplestuff to write here asieh 1flsidg

> dict=[{index:"1",caption:"AAAffterA",blurb:"stuff to write here asieh 1flsidg"}]
[ { index: '1',
    caption: 'AAAffterA',
    blurb: 'stuff to write here asieh 1flsidg' } ]
> dict.map ( function(a) { if (a.caption == "AAAffterA") return a.blurb } )
[ 'stuff to write here asieh 1flsidg' ]

      



Since an array can have multiple values, including nil, filter

across that array, return non-empty results and return the first non-empty result with [0]

.

>[ ,,,'stuff to write here asieh 1flsidg' ,,,].filter( function(a) {    
   if (a!=null) return a 
  } ) [0]
    'stuff to write here asieh 1flsidg'

      

Combine into one end function

> dict.map ( function(a) 
 { if (a.caption == "AAAffterA") 
     return a.blurb } 
 ).filter( function(a) { 
    if (a!=null) return a } 
  ) [0]
'stuff to write here asieh 1flsidg'

      

+1


source


Filter the array first to return only the element that has the desired signature, and then returns that one element if found:

var A = "AAAffterA",
    dict = [{index:"1",caption:"AAAffterA",blurb:"stuff to write here asieh 1flsidg"}];

// Return all matches for `A`,
var results = dict.filter(function(row){ return row.caption === A; });
// Get the first result, or a error message if none are found.
var found = results.length ? results[0].blurb : "Search returned no results!";

alert(found);
      

Run codeHide result


You can loop results

if multiple lines are found.

0


source


Can you use jQuery?

If so, use $grep

var dict=[{index:"1",caption:"AAAffterA",blurb:"stuff to write here asieh 1flsidg"}];

var results = $.grep(dict, function(e){ return e.caption == 'AAAffterA'; });

alert(results[0].blurb)

      

it will return an array of results matching the title in your array

-1


source







All Articles