1

I am trying to use array.filter() to filter an object array on the object names.

I've tried using array.constructor.name unsuccessfully.

var temp = ({ 
en: {
    id: `${scope}.en`,
    defaultMessage: 'English',
  },
  es: {
    id: `${scope}.es`,
    defaultMessage: 'Spanish',
  },
  ar: {
    id: `${scope}.ar`,
    defaultMessage: 'Arabic',
  },
});

var selectedObj = temp.filter(msg => msg.constructor.name === 'en');
0

1 Answer 1

1

The array.filter function only works on arrays. The Temp variable is not an array, it's an object. An array would be contained inside of square brackets []

It's not really clear to me what you're trying to do, but if your goal is to simply assign the "en" object to the selectedObj variable, then I believe the following will work:

var selectedObj = temp.en;

If you've got a variable that contains the string 'en', then maybe you could try:

var code = 'en';
var selectedObj = temp[code];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Joel. That worked for me! And also thank you for reminding me that the array.filter function only works on arrays and not objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.