1

I'm trying to "link" the Genre property in each of these objects inside this array to the title property that outputs to the user inside value.

var movies= [{
    "Title": "Platoon",
    "Genre": "War"
}, {
    "Title": "Pulp Fiction",
    "Genre": "Crime"
}];

var name = value;
//console.log(name) outputs as title the user clicked on just fine

var genre = ...["Genre"]; //no idea how to go about this
//this should say genre = ["Genre"] in object where ["Title"] is same as value 

Tried the IndexOf method to at least retrieve the element by searching for "Genre", but it outputs -1.

4
  • 1
    why not just iterate over the array and search for the title ? Commented Nov 2, 2016 at 15:56
  • Array.prototype.find() Commented Nov 2, 2016 at 15:57
  • 5
    I've read this five times, and I still don't get it? Do you want two arrays? Commented Nov 2, 2016 at 15:57
  • Can you include portion of javascript where value is set at user click event? Commented Nov 2, 2016 at 16:02

4 Answers 4

1

You could use Array#some.

function findGenre(title) {
    function search(a, i) {
        if (a.Title === title) {
            index = i;
            return true;
        }
    }
  
    var index;
    if (movies.some(search)) {
        return movies[index].Genre;
    }
}

var movies= [{ Title: "Platoon", Genre: "War" }, { Title: "Pulp Fiction", Genre: "Crime" }];

console.log(findGenre('Platoon'));

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

2 Comments

This answer works for me but one question: If "Title" in the object had spaces, I mean if the property was "Movie Title" instead of "Title", how would you apply that change to (a.Title === title) as well?
you could use the bracket notation: a.['Movie Title'] === title. please have a look to property accessors.
0

A simple for loop should do it.

var genre;
for(var i=0; i< movies.length; i++) {
  if (movies[i].Title === name) {
    genre = movies[i].Genre;
    break; // stop the loop assuming title is unique
  }
}

var movies= [{
    "Title": "Platoon",
    "Genre": "War"
}, {
    "Title": "Pulp Fiction",
    "Genre": "Crime"
}];

var name = 'Pulp Fiction';

var genre;
for(var i=0; i< movies.length; i++) {
  if (movies[i].Title === name) {
    genre = movies[i].Genre;
    break;
  }
}

alert(genre);

Comments

0

You can do,

  var found = movies.filter(function(m) { 
     return m.Title === value 
  });

  if (found.length > 0) { 
    console.log(found[0].Genre); // or handle some way different if more than one match found 
  }

Comments

0

You can use for..of loop, object destructuring to get values by checking if .innerHTML of clicked element is equal to value of Title property of object

var movies= [{
    "Title": "Platoon",
    "Genre": "War"
}, {
    "Title": "Pulp Fiction",
    "Genre": "Crime"
}];

var divs = document.querySelectorAll("div");
var [value, genre] = [null, null];
for (let el of divs) {
  el.onclick = (e) => {
    for (let props of movies) {
      let {Title, Genre} = props;
      if (Title === e.target.innerHTML) {   
        [value, genre] = [Title, Genre];
        console.log(`value:${value}, genre:${genre}`);
        break;
      }
    }
  }
}
<div>Platoon</div>
<div>Pulp Fiction</div>

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.