3

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.

4
  • Have you considered using Array.prototype.splice? 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. Commented Jun 15, 2014 at 13:03
  • slice vs. splice I'm talking about developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jun 15, 2014 at 13:59
  • oh yeah. I tried that one, but didn't know how to set the parameters. Commented Jun 15, 2014 at 14:07

2 Answers 2

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))

Sign up to request clarification or add additional context in comments.

7 Comments

is it possible to set the other values to null instead of deleting them?
solution: result = origin.slice(0, start2).concat(origin.slice(end2)).concat(origin.slice(start2,end2).map(function(item){return [item[0], 0]}));
Of course it's possible, but not by slice+concat alone. I'd recommend to only use the map and conditionally (based on the index) return null from the callback - it's just another transformation.
Are you re-ordering them on purpose? If not, you don't need any slices and concats: .map(function(item, index) { if (index >= start2 && index < end2) return [item[0], 0]; else return item; })
I think I had a blackout for the past 3 hours. got it adapted by result = origin.map(function(item, index) { if (index <= start || index >= end) return [item[0], 0]; else return item; })
|
1

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]

Comments

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.