2

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}
4
  • 1
    Possible duplicate of In javascript, how do you search an array for a substring match Commented Jul 23, 2016 at 4:01
  • 2
    value is an object at particular index of arrray. You may need to do value.name Commented Jul 23, 2016 at 4:04
  • also you need to use newArr.push(result); Commented Jul 23, 2016 at 4:06
  • Don't break; if you want newArr to store more than just the first match. Commented Jul 23, 2016 at 4:06

2 Answers 2

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";
});
Sign up to request clarification or add additional context in comments.

3 Comments

The first 4 characters would be under substring(0, 4)
you give value to name, then check for value.substring, also when substring(0,5) returns word with whitespace "Slow " so it won't match "Slow"
Yup you are right, my bad. Was editing his code, didn't pay attention. Thanks, edited
1

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"

1 Comment

indexOf will look at the whole string. If they just want to look at the first 4 characters, they should use slice(0, 4) === "Slow"

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.