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

javascript - Push to an Array using a for loop - Stack Overflow

programmeradmin1浏览0评论

im a JS newbie. Trying to figure out this problem below. I am stuck on the "if" statement and not sure exactly what to put in. Also im not sure if my "push" is setup right

// Define a function named `pact` that accepts an array and returns
// another array with all the falsey values removed from it. For example,
// if this array was passed in:
//   [1, 4, 0, '', undefined, false, true, null, {mj: 'mj'}, 'Hello']
// the function would return this, as a result:
//   [1, 4, true, {mj: 'mj'}, 'Hello']

var pact = function (array) {
    var truthyValues = [];
    for (var i = 0; i < array.length; i += 1) {
        if () {
            truthyValues.push[i];
        }
    }
    return truthyValues;
};

im a JS newbie. Trying to figure out this problem below. I am stuck on the "if" statement and not sure exactly what to put in. Also im not sure if my "push" is setup right

// Define a function named `pact` that accepts an array and returns
// another array with all the falsey values removed from it. For example,
// if this array was passed in:
//   [1, 4, 0, '', undefined, false, true, null, {mj: 'mj'}, 'Hello']
// the function would return this, as a result:
//   [1, 4, true, {mj: 'mj'}, 'Hello']

var pact = function (array) {
    var truthyValues = [];
    for (var i = 0; i < array.length; i += 1) {
        if () {
            truthyValues.push[i];
        }
    }
    return truthyValues;
};
Share Improve this question edited Dec 6, 2013 at 20:16 Mörre 5,7236 gold badges40 silver badges66 bronze badges asked Dec 6, 2013 at 20:13 jstonejstone 4453 gold badges8 silver badges19 bronze badges 1
  • 1 Read this - meaning of "falsy" and "truthy" values: sitepoint./javascript-truthy-falsy Non-Boolean values are converted to "true" or "false" when a Boolean value is needed, and how that is done depends on some rules. In other, more type-strict languages you could not do that but would be forced to declare explicitly whether you want "true" or "false". – Mörre Commented Dec 6, 2013 at 20:18
Add a ment  | 

4 Answers 4

Reset to default 4

You're close. Here's what the if should be:

if (array[i]) {
    truthyValues.push(array[i]);
}

Since you want to check for truthiness of each array element, just put array[i] in the if block. That gets the value of the array at i, and will evaluate as true if that value is truthy.

Then push not i - the index into array - but array[i], so that the actual value is in truthyValues.

Just put the value you want to check is truthy in the if statement, since the if statement checks for truthiness.

if(array[i]){
  truthyValues.push(array[i]);
}

I think you can just use:

if (array[i]) { truthyValues.push(array[i]); }

Values like null will trigger false.

if (typeof array[i] !== "undefined" && array[i] !== null) {// push to array // }

most of these other answers will not work for falsy and existy values. You'll need to define what you intend to be existy and write a helper function for that. you could use the one I provided as a start and then roll your own.

发布评论

评论列表(0)

  1. 暂无评论