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

jquery - Changing array value when condition is met in javascript - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jun 2, 2013 at 10:49 ejx asked Jun 2, 2013 at 10:25 ejxejx 4891 gold badge6 silver badges22 bronze badges 2
  • Your code seems alright. Except for a missing closing brace. While the first code snippet is missing a closing bracket. – kirelagin Commented Jun 2, 2013 at 10:27
  • 1 Also note that according to your comment, you are going to print each modified value twice. – kirelagin Commented Jun 2, 2013 at 10:29
Add a comment  | 

4 Answers 4

Reset to default 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] ); 
});

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];

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 }.

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

发布评论

评论列表(0)

  1. 暂无评论