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

Slice and map javascript array on multiple items - Stack Overflow

programmeradmin1浏览0评论

I have a multidimensional array like:

profile = [[2001-05-01, 20], [2001-05-02, 23], [2001-05-03, 18], ...];

I create a new array and manipulate the second element of the array by a variable factor.

I'm doing this right now and it works:

function new_ar(origin, start, end, factor) {
  var result = [[]];
  result = origin.slice(start, end).map(function(item){ return [item[0], parseInt(item[1])*factor]});
  return result;
};

I get the desired array:

array2 = [[2001-05-01, 400], [2001-05-02, 460], [2001-05-02, 360], ...]

Problem

How can I get a new array3, which consists of the first n and last m elements of array2? I need to call a function like

function new_ar(origin, start1, end1, factor){
...code...
}

for example: I have an array with 200 elements, I need to get a new array with the first 100 and last 50. That means, that I need to say delete from 100 to 149.

I have a multidimensional array like:

profile = [[2001-05-01, 20], [2001-05-02, 23], [2001-05-03, 18], ...];

I create a new array and manipulate the second element of the array by a variable factor.

I'm doing this right now and it works:

function new_ar(origin, start, end, factor) {
  var result = [[]];
  result = origin.slice(start, end).map(function(item){ return [item[0], parseInt(item[1])*factor]});
  return result;
};

I get the desired array:

array2 = [[2001-05-01, 400], [2001-05-02, 460], [2001-05-02, 360], ...]

Problem

How can I get a new array3, which consists of the first n and last m elements of array2? I need to call a function like

function new_ar(origin, start1, end1, factor){
...code...
}

for example: I have an array with 200 elements, I need to get a new array with the first 100 and last 50. That means, that I need to say delete from 100 to 149.

Share Improve this question edited Jun 15, 2014 at 11:34 gco asked Jun 15, 2014 at 11:05 gcogco 1,7007 gold badges24 silver badges46 bronze badges 4
  • Have you considered using Array.prototype.splice? – Oleg Commented Jun 15, 2014 at 12:08
  • Hi Oleg, I'm using slice already as you can see in `function new_ar´. As far as I understand slice, it gets me a continuois portion of an array. for example n to m. But I need to get n to m and x to y. – gco Commented Jun 15, 2014 at 13:03
  • slice vs. splice I'm talking about developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Oleg Commented Jun 15, 2014 at 13:59
  • oh yeah. I tried that one, but didn't know how to set the parameters. – gco Commented Jun 15, 2014 at 14:07
Add a ment  | 

2 Answers 2

Reset to default 3

Simply slice out the portions of the array you want and then concat them!

…origin.slice(0, start1).concat(origin.slice(end1))…

(Assuming end1 is 150 in your example; if it's 50 then use .slice(-end1))

This should get you there.

The multidimensional array:

profile = [['test_start', 20], ['test_start', 23], [2001-05-03, 18],[2001-05-01, 20], [2001-05-02, 23], [2001-05-03, 18], [2001-05-03, 18],[2001-05-01, 20], [2001-05-02, 23], ['test_end', 18]];

a map function for iterating over the array and returning only the matched results. To be called in our segmentArray function:

array_map = function(array, mapFunction) {
          var newArray = new Array(array.length);
          for(var i = 0; i < array.length; i++) {
              response = mapFunction(array[i]);
              if(response){
                    newArray[i] = response;
              }
          }
          return newArray;
}

Our segmentArray function to call on the array:

 Array.prototype.segmentArray = function(start1, end1, factor){

   var array_length = this.length;
   var a = 0;
   var end1 = array_length - end1;
   var array = array_map(this, function(v,i){
       a++;
      return a <= start1 || a > end1 ? v : false;
   });
     return array;

}

Usage:

new_arr = profile.segmentArray(start1=2, end1=1, factor=null);
console.log(new_arr);

The result, the first 2 and the last 1 items from the array, as we set in the passed function params:

0: Array[2] 
1: Array[2]
2: Array[2]
发布评论

评论列表(0)

  1. 暂无评论