Ceddy Muhoza
Ceddy Muhoza

Reputation: 636

Javascript, loop through array and apply other functions

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 ?

Upvotes: 0

Views: 501

Answers (2)

Damon
Damon

Reputation: 4346

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

Upvotes: 1

Vladu Ionut
Vladu Ionut

Reputation: 8193

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

Upvotes: 0

Related Questions