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

concat array after filter javascript - Stack Overflow

programmeradmin1浏览0评论

this code doesn't work

 var arr1 = ["a", "b", "c"];
 var arrayJoin=[];
arrayJoin.concat(arr1.filter(function (val) {
                    return val == "a";
    }));

console.log(arrayJoin);

this works fine

arrayJoin=arr1.filter(function (val) {
                    return val == "a";
    }));

any idea how to concat the filter array?

this code doesn't work

 var arr1 = ["a", "b", "c"];
 var arrayJoin=[];
arrayJoin.concat(arr1.filter(function (val) {
                    return val == "a";
    }));

console.log(arrayJoin);

this works fine

arrayJoin=arr1.filter(function (val) {
                    return val == "a";
    }));

any idea how to concat the filter array?

Share Improve this question asked Dec 14, 2016 at 20:43 baarozbaaroz 19.6k8 gold badges38 silver badges48 bronze badges 3
  • 3 Array.prototype.concat() [...]The concat() method is used to merge two or more arrays. This method does **not** change the existing arrays, but instead **returns a new** array.[...] – t.niese Commented Dec 14, 2016 at 20:47
  • 1 Array.prototype.filter() returns a new Array, so what do you need concat for – DavidDomain Commented Dec 14, 2016 at 20:49
  • this is one concat in a series – baaroz Commented Dec 14, 2016 at 20:51
Add a ment  | 

1 Answer 1

Reset to default 5

What about

arrayJoin = arrayJoin.concat(arr1.filter(function (val) {
                    return val == "a";
    }));
发布评论

评论列表(0)

  1. 暂无评论