-1

From the below array of objects I am trying to return the value of the firstName Key and the country key from the first object found where the language value is set to Python, I tried the find function but I think that's just for values and the tried the filter function below with no joy, any help would be much appreciated. Kind regards, Jon

function getFirstPython(list) {
    var python =  list.filter(function(el) {
        return el.lanaguage = "Python";
    });
}
Andreas
  • 21,535
  • 7
  • 47
  • 56
  • 1
    el.lanaguage === "Python" – Majed Badawi Aug 21 '21 at 10:32
  • _"I tried the find function but I think that's just for values"_ - The `.find()` method will return the first element of the array that satisfies the given predicate (callback). It's the same setup as with `.filter()` with the only difference, that `.filter()` returns _all_ elements of the array that satisfy the given predicate. – Andreas Aug 21 '21 at 10:35
  • Thanks Majed I put the explicitly equals operator in but it still returns undefined – Jonathan Joseph Aug 21 '21 at 10:36
  • `=` is the assignment operator. You want `==` or `===` for comparing two values. – Andreas Aug 21 '21 at 10:36
  • _"...but it still returns undefined"_ - Because `getFirstPython()` doesn't `return` anything. -> `return python;` – Andreas Aug 21 '21 at 10:37
  • single equal `=` is assignment operator, 2 equals `==` is for comparison, 3 equals `===` is for strict comparison. It's different @JonathanJoseph – Dhana D. Aug 21 '21 at 10:37
  • Yeah I need to find the first occurance of the Python value, return that object from the array and the pull the first Name and country key values out and return them. – Jonathan Joseph Aug 21 '21 at 10:39
  • Thanks Andrea's I see that now I returned in one function but not in the other now I get the element I want but not all of the object it's contained within. Any ideas? – Jonathan Joseph Aug 21 '21 at 10:46
  • Hi @JonathanJoseph I have put an answer below. Does it help you solving this problem? – Dhana D. Aug 21 '21 at 10:48

2 Answers2

1

You can divide it to 3 steps in your function:

  1. Get the objects that having its language === "Python". This will return an array.
  2. Get the first element of the array.
  3. Put the firstName and country on the function's return.

Here's the code if you wish it returns as Object:

function getFirstPython(list) {
    var python =  list.filter(function(el) { // Step 1
        return el.language === "Python";
    });
    python = python[0]; // Step 2
    return {firstName: python.firstName, country: python.country}; // Step 3
}

var array = [
  {language: "Python", firstName: "A", country: "US"},
  {language: "C++", firstName: "B", country: "UK"},
  {language: "Python", firstName: "C", country: "AF"},
];

console.log(getFirstPython(array));

Here's the code if you wish it returns as Array:

function getFirstPython(list) {
    var python =  list.filter(function(el) { // Step 1
        return el.language === "Python";
    });
    python = python[0]; // Step 2
    return [python.firstName, python.country]; // Step 3
}

var array = [
  {language: "Python", firstName: "A", country: "US"},
  {language: "C++", firstName: "B", country: "UK"},
  {language: "Python", firstName: "C", country: "AF"},
];

console.log(getFirstPython(array));
Dhana D.
  • 1,670
  • 3
  • 9
  • 33
  • Cheer mate that works a treat apart from I need to drop the firstName and country characters so I only return Victoria, Puerto Rico, any ideas? – Jonathan Joseph Aug 21 '21 at 11:24
  • You intend to make it as array? I added up the options to my answer @JonathanJoseph – Dhana D. Aug 21 '21 at 14:03
  • Hi @JonathanJoseph. If you find that my answer is helpful, please kindly accept my answer. https://stackoverflow.com/help/someone-answers – Dhana D. Aug 22 '21 at 13:44
0

I hope this code helping you

var array = [
  {language: "Python", firstName: "A", country: "US"},
  {language: "C++", firstName: "B", country: "UK"},
  {language: "Python", firstName: "C", country: "AF"},
];
var result = array.filter(ele=>ele.language == "Python")[0]
console.log(result.firstName, result.country)
Viktor M
  • 301
  • 1
  • 7
  • Cheers mate thats great but I need to pass the first.Name and country key values to a return statement and not a console.log this is the code I have up to now that returns the correct object from the array, just struggling to return the firstName and country keys ' let python = list.filter(el => el.language === 'Python'); return(python)' – Jonathan Joseph Aug 21 '21 at 11:03