0

I have an array of objects,

var out = [{
    "type": "1",
    "from": "13052033555",
    "to": "4444444",
    "amount": "40000",
    "date": 1461575799,
    "status": "1"
  }, {
    "type": "2",
    "from": "13052033555",
    "to": "1111111",
    "amount": "30000",
    "date": 1461575884,
    "status": "1"
  }...
];

I get only it's values without keys

Now i used this function to get the values from array like this, I pass array then it returns only values without keys

function foo(a) {
  var values = [];
  for (var i = 0; i < a.length; i++) {
    var obj = a[i];
    var arr = Object.keys(obj).map(function(k) {
      return obj[k]
    });
    values.push("[" + arr + "],");
  }
  return values.join('');
}

Then it returns the values data without keys like this,

["1","13052033555","4444444","40000",1461575799,"1"],
["2","13052033555","1111111","30000",1461575884,"1"],

My question is how can I loop through the values and only apply a method to 5th value of each data array ?

Rayon
  • 36,219
  • 4
  • 49
  • 76
Ceddy Muhoza
  • 636
  • 3
  • 17
  • 34
  • `["1","13052033555","4444444","40000",1461575799,"1"].map(function(item, index){ if(index===4){ return item * 100; } return item; })` – Rayon Jun 28 '16 at 12:02
  • Second argument in `Array#map` is `index`..Use it..It could be manipulated in `Object.keys(obj).map` as well! – Rayon Jun 28 '16 at 12:03
  • 1
    So, you have an array of objects, which you convert to an array of arrays (correct me if I'm wrong) and you want, after the conversion to go through each array in the array of arrays and do something to the 5th element of the (inner) array? – apokryfos Jun 28 '16 at 12:04
  • @apokryfos Yes that's right! – Ceddy Muhoza Jun 28 '16 at 12:06
  • 1
    @Tuna — https://jsfiddle.net/rayon_1990/Lq4djx1s/ – Rayon Jun 28 '16 at 12:11

2 Answers2

1

All you have to do in foo is call your fn on arr[4] before continuing with the loop.

arr[4] = fn(arr[4])

This is of course, assuming you don't need to do this after the fact for whatever reason. If that is the case, you can use another for loop like your original code, and just modify the 5th element in each array as specified above, except it would look more like

for (var i = 0; i < outerArray.length; i++) {
  outerArray[i][4] = fn(outerArray[i][4])
}

Now that we covered how to do it with your current code, I would also suggest that you don't do this for any real world application. If you want to modify data on a specific object property for a list of objects, you should do it with the object property name (key) and not later on the array of values using an index. This prevents any confusion that could arise from the fact that objects do not have a guaranteed order. Here's an example of how I would do this, assuming you want to modify date:

function foo(a) {
  return a.map(function(obj) {
    return Object.keys(obj).map(function(k) {
      return k === 'date' ? fn(obj[k]) : obj[k]
    })
  })
}

This way you target the property you want but also don't modify the original object.

Note You should replace fn with your desired function :)

Edit Per your request, here is how you might extend it to check for other property names

function foo(a) {
  return a.map(function(obj) {
    var values = []
    Object.keys(obj).forEach(function(k) {
      if (k === 'date') {
        values.push(fn(obj[k]))
      } else if (k !== 'type') {
        values.push(obj[k])
      }
    })

    return values
  })
}
Damon
  • 4,216
  • 2
  • 17
  • 27
  • i used the second method to modify date, i had another problem... How do i skip the the `type` key and remove it from the generated arrays – Ceddy Muhoza Jun 28 '16 at 16:50
  • @Tuna Instead of checking for just `date` you can use an `if.. else if .. else` statement to check for `date`, do something, check for `type` do something else, otherwise just do the normal behavior – Damon Jun 28 '16 at 16:58
  • can you please paste a simple example would really help as i have been trying but failed. thanks – Ceddy Muhoza Jun 28 '16 at 17:38
0

var out = [{
    "type": "1",
    "from": "13052033555",
    "to": "4444444",
    "amount": "40000",
    "date": 1461575799,
    "status": "1"
  }, {
    "type": "2",
    "from": "13052033555",
    "to": "1111111",
    "amount": "30000",
    "date": 1461575884,
    "status": "1"
  }
];

function foo(a) {
  var values = [];
  for (var i = 0; i < a.length; i++) {
    var obj = a[i];
    var arr = Object.keys(obj).map(function(k) {
      return obj[k]
    });
    values.push("[" + arr + "],");
  }
  return values.join('');
}

function myFn(elem){
  console.log(elem);
  return elem;
}

var arr = foo(out);
arr = JSON.parse("["+arr.substring(0,arr.length-1)+"]")

arr.forEach(function(elem){
  return myFn(elem[4]);
});
console.log(arr);
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30