7

Let's say I have an array var arr = [3,4,5,6,7,9]; and I log the contents like this:

$.each(arr, function(k, v) {
  console.log(v);
}

As I log the contents I want to check if the current value is bigger than for example var limit = 5;.

If the current value is bigger than limit I want to replace/change that value to let's say the letter A and print it out as such. So my logged arr array would look like this 3,4,5,A,A,A.

I was thinking about something like this:

$.each(arr, function(k,v) {
  if (v > limit) {
    // set this specific value equal to limit
    // log changed value
  }
  console.log(v); // otherwise just log the value found
});

I tried this, but it does nothing, no errors either.

2
  • Your code seems alright. Except for a missing closing brace. While the first code snippet is missing a closing bracket. Commented Jun 2, 2013 at 10:27
  • 1
    Also note that according to your comment, you are going to print each modified value twice. Commented Jun 2, 2013 at 10:29

4 Answers 4

9

JSFIDDLE: http://jsfiddle.net/nsgch/8/

var arr = [3,4,5,6,7,9];
var limit = 5;

$.each(arr, function(k,v) {
  if (v > limit) {
       arr[k] = 'A'; 
  }
  console.log( arr[k] ); 
});
Sign up to request clarification or add additional context in comments.

Comments

5

You can simply write somethig like this to handle these scenarios...

Imagine you have:

const arr = [0, 1, 6, 12, 0, 78];

Use something like:

arr.map(a => a === 0 ? "a" :a);

and the result will be:

["a", 1, 6, 12, "a", 78];

Comments

2

It depends how you were doing the "set this specific value equal to limit". If you were doing;

$.each(arr, function(k,v) {
  if (v > limit) {
    v = "A";
    // log changed value
  }
  console.log(v); // otherwise just log the value found
});

You were changing only the local variable v, rather than the element arr[k]. You can either update arr[k] like in @san.chez answer, or use $.map;

var filtered = $.map(arr, function(v,k) {
  if (v > limit) {
    return "A";
  }

  return v;
});

... then filtered would be your array [1,2,4,A,A], and arr would be unchanged. Note the swapping of k and v parameters; jQuery is consistent like that /sarcasm


Note also that both of your code samples are missing the closing }.

Comments

1
var arr = [3,4,5,6,7,9];
arr=arr.map(function(elem){ return elem>5?"A":elem; });
arr.forEach(function(elem){
    console.log(elem);
})

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

3 Comments

It's worth noting that Array.prototype.map is an ES5 method, and not cross-browser; the ES5 shim can be added to ensure support for older browsers
As far as I can see, there is a compatibility info in the link I posted. And in case you have an older browser, you can download a new one for free :)
I know there's a compatibility section in the link you posted (the shim I linked to links to that section!), but just for the drive-byers who don't click your link, it's important to note.

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.