0

My question is, how can I find all array indexes by a word ?

[
{name: "Jeff Crawford", tel: "57285"},
{name: "Jeff Maier", tel: "52141"},
{name: "Tim Maier", tel: "73246"}
]

If I search for "Jeff", I want to get:

[
{name: "Jeff Crawford", tel: "57285"},
{name: "Jeff Maier", tel: "52141"},
]
3
  • 2
    You have anything that you tried? Commented May 14, 2018 at 6:21
  • You want only indices or the elements? Commented May 14, 2018 at 6:21
  • Please visit the help center, take the tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output. Commented May 14, 2018 at 6:26

4 Answers 4

2

To make it more versatile, you could take a function which takes an array of objects, the wanted key and the search string, wich is later used as lower case string.

function find(array, key, value) {
    value = value.toLowerCase();
    return array.filter(o => o[key].toLowerCase().includes(value));
}

var array = [{ name: "Jeff Crawford", tel: "57285" }, { name: "Jeff Maier", tel: "52141" }, { name: "Tim Maier", tel: "73246" }]

console.log(find(array, 'name', 'Jeff'));

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

Comments

0

Use .filter:

const input=[{name:"Jeff Crawford",tel:"57285"},{name:"Jeff Maier",tel:"52141"},{name:"Tim Maier",tel:"73246"}]
const filtered = input.filter(({ name }) => name.startsWith('Jeff'));
console.log(filtered);

If you want to check to see if "Jeff" is anywhere rather than only at the beginning, use .includes instead:

const input=[{name:"Jeff Crawford",tel:"57285"},{name:"foo-Jeff Maier",tel:"52141"},{name:"Tim Maier",tel:"73246"}]
const filtered = input.filter(({ name }) => name.includes('Jeff'));
console.log(filtered);

These are ES6 features. For ES5, use indexOf instead:

var input=[{name:"Jeff Crawford",tel:"57285"},{name:"foo-Jeff Maier",tel:"52141"},{name:"Tim Maier",tel:"73246"}]
var filtered = input.filter(function(obj){
  return obj.name.indexOf('Jeff') !== -1;
});
console.log(filtered);

Comments

0

Use Array#map on the original array and return the indices. Then filter out the undefined values:

const data = [{name:"Jeff Crawford",tel:"57285"},{name:"Jeff Maier",tel:"52141"},{name:"Tim Maier",tel:"73246"}];

const indices = data.map((item, i) => {
    if (item.name.startsWith('Jeff')) {
      return i;
    }
  })
  .filter(item => item !== undefined);

console.log(indices);

Comments

0

Use array filter method along with indexOf. filter will return a new array of matched element. In side the filter callback function check for the name where the indexOf the keyword is not -1. That is the name should contain the keyword

var originalArray = [{
    name: "Jeff Crawford",
    tel: "57285"
  },
  {
    name: "Jeff Maier",
    tel: "52141"
  },
  {
    name: "Tim Maier",
    tel: "73246"
  }
]

function getMatchedElem(keyWord) {
  return originalArray.filter(function(item) {
    return item.name.indexOf(keyWord) !== -1

  })

}

console.log(getMatchedElem('Jeff'))

Comments

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.