最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Searching Array for partial string - Stack Overflow

programmeradmin6浏览0评论

Have a function that returns an array of objects. The array has a rate object that has a name field. Inside the name field are names such as "Slow speed" and "Fast speed".

I have written the following in hopes to create a new array that will filter out the array values and just return only those with "Slow" that matches from the rates[i].name.

So far I am encountering this error in my dev console. "Uncaught TypeError: value.substring is not a function"

var rates = myArray();
var index, value, result;
var newArr = [];

for (index = 0; index < rates.length; ++index) {
    //value = rates[index];
    if (value.substring(0, 5) === "Stand") {
        result = value;
        newArr.push();
        break;
    }
}

Part of array return in console.

"rates":[{"id":1123,"price":"1.99","name":"Slow speed - Red Car","policy":{"durqty":1,"durtype":"D","spdup":15000,"spddwn":15000}

Have a function that returns an array of objects. The array has a rate object that has a name field. Inside the name field are names such as "Slow speed" and "Fast speed".

I have written the following in hopes to create a new array that will filter out the array values and just return only those with "Slow" that matches from the rates[i].name.

So far I am encountering this error in my dev console. "Uncaught TypeError: value.substring is not a function"

var rates = myArray();
var index, value, result;
var newArr = [];

for (index = 0; index < rates.length; ++index) {
    //value = rates[index];
    if (value.substring(0, 5) === "Stand") {
        result = value;
        newArr.push();
        break;
    }
}

Part of array return in console.

"rates":[{"id":1123,"price":"1.99","name":"Slow speed - Red Car","policy":{"durqty":1,"durtype":"D","spdup":15000,"spddwn":15000}
Share Improve this question edited Jul 23, 2016 at 4:03 castletheperson 33.5k11 gold badges74 silver badges107 bronze badges asked Jul 23, 2016 at 3:59 VanCoonVanCoon 4315 silver badges24 bronze badges 4
  • 1 Possible duplicate of In javascript, how do you search an array for a substring match – Rathma Commented Jul 23, 2016 at 4:01
  • 2 value is an object at particular index of arrray. You may need to do value.name – Ajay Narain Mathur Commented Jul 23, 2016 at 4:04
  • also you need to use newArr.push(result); – moped Commented Jul 23, 2016 at 4:06
  • Don't break; if you want newArr to store more than just the first match. – castletheperson Commented Jul 23, 2016 at 4:06
Add a ment  | 

2 Answers 2

Reset to default 4

You have an object at each array location not the string itself, try this instead:

var rates = myArray();
var index, value, result;
var newArr = [];

for (index = 0; index < rates.length; ++index) {
    name = rates[index].name;
    if (name.substring(0, 4) === "Slow") {
        newArr.push(rates[index]);
    }
}

Try using filter function like this, it is much more cleaner to see

var newArr = rates.filter(function(rate){
  return rate.name && rate.name.substring(0,4) === "Slow";
});

You can use filter to do this, for example:

var newArr = rates.filter(function(val){
    // check if this object has a property `name` and this property's value starts with `Slow`.
    return val.name && val.name.indexOf("Slow") == 0; 
});

As @4castle mentioned, instead of indexOf(...) you can use slice(...) which may be more efficent, eg: val.name.slice(0,4) == "Slow"

发布评论

评论列表(0)

  1. 暂无评论