0

I've got an array of objects

var arr1 = [{
        "amount": 700,
        "hits": 4,
        "day": 1
    },
    {
        "amount": 100,
        "hits": 7,
        "day": 4
    },
    {
        "amount": 500,
        "hits": 3,
        "day": 8
    }
];

What I need is to create a new array of objects, with key "param" in each object, and the value of "param" should be equals to "amount" field in arr1 objects.

Expected output:

var newArr = [{
        "param": 700
    },
    {
        "param": 100
    },
    {
        "param": 500
    }
];
1
  • 4
    var newArr = arr1.map(o => ({param: o.amaunt})); Commented Aug 17, 2016 at 13:10

6 Answers 6

3

You can user Array.prototype.map():

var arr1 = [{"amaunt": 700,"hits": 4,"day": 1}, {"amaunt": 100,"hits": 7,"day": 4}, {"amaunt": 500,"hits": 3,"day": 8}],
    newArr = arr1.map(function(elem) {
        return {param: elem.amaunt};
    });

console.log(newArr);

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

Comments

2

Try using map:

var arr1 = [
  {
    "amaunt": 700,
    "hits":4,
    "day":1
  },
  {
    "amaunt": 100,
    "hits":7,
    "day":4
  },
  {
    "amaunt": 500,
    "hits":3,
    "day":8
  }
];
var arr2 = arr1.map(function(obj) {
  return {param: obj.amaunt};
});
console.log(arr2);

Comments

2
var newArr = []
arr1.forEach(obj => {
   newArr.push({param: obj.amount});
});

Comments

1

You can try this

var arr1 = [
            {
                "amaunt": 700,
                "hits":4,
                "day":1
            },
            {
                "amaunt": 100,
                "hits":7,
                "day":4
            },
            {
                "amaunt": 500,
                "hits":3,
                "day":8
            }
        ];

    var newArr=[];
    arr1.forEach(function(item){
        newArr.push({"param":item.amaunt});
    });
    console.log(newArr);

Comments

0

Try this

arr1.forEach(function(data){            
          var obj = {"param": data.amount};          
          newArr.push(obj);          
        })

Fiddle here

Comments

0

Another way you can do this

var newArr=[];  
arr1.map(function(obj) {
newArr.push("param:"+obj.amaunt);
 });

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.