Home > OS >  Finding items from an array of objects based on a selected item value in JS
Finding items from an array of objects based on a selected item value in JS

Time:01-06

I have an array like this:

var array = [
    { name:"string123", value:"this", other: "that" },
    { name:"string2", value:"this", other: "that" },
    { name:"string12", value:"ths", other: "that" },
];

And now I have a string like 'string12'. Now with this string the output I want is to find the items that contains this string which is like:

var array = [
    { name:"string123", value:"this", other: "that" },
    { name:"string12", value:"ths", other: "that" },
];

Here's what I have tried:

const search = (nameKey, myArray) => {
  for (var i = 0; i < myArray.length; i  ) {
    var newArr = [];
    if (myArray[i].name.slice(0, nameKey.length   1) === nameKey) {
      newArr.push(myArray[i]);
    }
    return newArr;
  }

}

var array = [
  { name:"string123", value:"this", other: "that" },
  { name:"string2", value:"this", other: "that" },
  { name:"string12", value:"ths", other: "that" },
];

var resultObject = search("string12", array);
console.log(resultObject);

But my solution is returning a blank array. Where am I going wrong with this?

CodePudding user response:

The following things went wrong in your implementation:

  1. Move initialization outside the for loop.
  2. You don't need 1 when doing .slice().
  3. Return statement return newArr should be outside the for loop.
const search = (nameKey, myArray) => {
    var newArr = [];
  
  for (var i = 0; i < myArray.length; i  ) {
    if (myArray[i].name.slice(0, nameKey.length) === nameKey) {
      newArr.push(myArray[i]);
    }
  }
    return newArr;
}

var array = [
  { name:"string123", value:"this", other: "that" },
  { name:"string2", value:"this", other: "that" },
  { name:"string12", value:"ths", other: "that" },
];

var resultObject = search("string12", array);
console.log(resultObject);

CodePudding user response:

Using Array#filter and String#includes:

const arr = [ { name:"string123", value:"this", other: "that" }, { name:"string2", value:"this", other: "that" }, { name:"string12", value:"ths", other: "that" } ];

const res = arr.filter(({ name }) => name.includes('string12'));

console.log(res);

You can use String#startsWith instead of includes if you only need to check if the name starts with the string.

CodePudding user response:

You could do this with Array.filter and startsWith function (docs). If you want to check string12 anywhere in name use includes instead of startsWith

var array = [
    { name:"string123", value:"this", other: "that" },
    { name:"string2", value:"this", other: "that" },
    { name:"string12", value:"ths", other: "that" },
];

const filtered = array.filter(item => item.name.startsWith("string12"));

console.log(filtered);

CodePudding user response:

If you're just searching for string at start then removing 1 form myArray[i].name.slice(0,nameKey.length 1) will work for you then only it will match the exact string you're searching for and also you have to declare newArr above the loop so it can hold the value and return it after the loop ends hope it helps

    const search = (nameKey, myArray)=> {
    var newArr = [];
    for (var i=0; i < myArray.length; i  ) {
        if (myArray[i].name.slice(0,nameKey.length) === nameKey) {
            newArr.push(myArray[i]);
        }
    }
    return newArr;
}

var array = [
    { name:"string123", value:"this", other: "that" },
    { name:"string2", value:"this", other: "that" },
    { name:"string12", value:"ths", other: "that" },
    { name:"string1234567", value:"ths", other: "that" },
];

var resultObject = search("string12", array);
console.log(resultObject);

You can also use modern syntax for this problem

const search = (nameKey, myArray) => {
  return myArray.filter(elem => elem.name.startsWith(nameKey));
};

var array = [
  { name: "string123", value: "this", other: "that" },
  { name: "string2", value: "this", other: "that" },
  { name: "string12", value: "ths", other: "that" },
];

var resultObject = search("string12", array);
console.log(resultObject);
  •  Tags:  
  • Related