0
[
  {

    "user_pass": "$PBa5$.cxL91nBU5cG4gqhNp8mWZoJgFY/",
    "user_mobile":"1234567890",
    "user_nicename": "abc",
    "user_email": "[email protected]",
    "user_status": 0

  },
  {

    "user_pass": "$P$BwfQRzajR6R9eLeZbPnTgfQfsfPDhK0",
    "user_mobile":"0987654321",
    "user_nicename": "cdv",
    "user_email": "[email protected]",
    "user_status": 0
  },
  {

    "user_pass": "$P$Be15Zwqze.9OxoYfTOMd0WjlgXO7xe.",
    "user_mobile":"5432167890",
    "user_nicename": "zxc",
    "user_email": "[email protected]",
    "user_status": 0
  }
 ]

Above is my data.

1 . I want to delete user_pass field in every object.

2 . I want to use user_mobile as user_pass

my expectation result:

below i have mentioned my final result

[
  {

    "user_pass": "1234567890",
    "user_mobile":"1234567890",
    "user_nicename": "abc",
    "user_email": "[email protected]",
    "user_status": 0

  },
  {

    "user_pass": "0987654321",
    "user_mobile":"0987654321",
    "user_nicename": "cdv",
    "user_email": "[email protected]",
    "user_status": 0
  },
  {

    "user_pass": "5432167890",
    "user_mobile":"5432167890",
    "user_nicename": "zxc",
    "user_email": "[email protected]",
    "user_status": 0
  }
 ]

any one help me out i am new to this technology .

3
  • Use simple and humble for loop, iterate array and update user_pass. Commented Nov 30, 2016 at 5:57
  • can you please help me out @Satpal Commented Nov 30, 2016 at 5:58
  • jsfiddle.net/as2kg67u for (var i in users) { users[i].user_pass= users[i].user_mobile; } Commented Nov 30, 2016 at 5:59

6 Answers 6

1

You have to loop through the object array and reference user_mobile to user_pass.

var arr = [...data...];

arr.forEach(function(obj) {
    obj.user_pass = obj.user_mobile;
});
Sign up to request clarification or add additional context in comments.

Comments

0

forEach

var arr=[ { "user_pass": "$PBa5$.cxL91nBU5cG4gqhNp8mWZoJgFY/", "user_mobile":"1234567890", "user_nicename": "abc", "user_email": "[email protected]", "user_status": 0 }, { "user_pass": "$P$BwfQRzajR6R9eLeZbPnTgfQfsfPDhK0", "user_mobile":"0987654321", "user_nicename": "cdv", "user_email": "[email protected]", "user_status": 0 }, { "user_pass": "$P$Be15Zwqze.9OxoYfTOMd0WjlgXO7xe.", "user_mobile":"5432167890", "user_nicename": "zxc", "user_email": "[email protected]", "user_status": 0 } ];
arr.forEach(function(a){
a.user_pass=a.user_mobile;
})
console.log(arr);

Comments

0

var data = [{

  "user_pass": "$PBa5$.cxL91nBU5cG4gqhNp8mWZoJgFY/",
  "user_mobile": "1234567890",
  "user_nicename": "abc",
  "user_email": "[email protected]",
  "user_status": 0

}, {

  "user_pass": "$P$BwfQRzajR6R9eLeZbPnTgfQfsfPDhK0",
  "user_mobile": "0987654321",
  "user_nicename": "cdv",
  "user_email": "[email protected]",
  "user_status": 0
}, {

  "user_pass": "$P$Be15Zwqze.9OxoYfTOMd0WjlgXO7xe.",
  "user_mobile": "5432167890",
  "user_nicename": "zxc",
  "user_email": "[email protected]",
  "user_status": 0
}];

var newData = data.map(function(item) {
  return {
    user_pass: item.user_mobile,
    user_mobile: item.user_mobile,
    user_nicename: item.user_nicename,
    user_email: item.user_email,
    user_status: item.user_status
  };
});

console.log(newData);

1 Comment

0

If you need to do it in an immutable way you could map over the array and Assign all the object properties to new objects.

const data = [
  {

    "user_pass": "$PBa5$.cxL91nBU5cG4gqhNp8mWZoJgFY/",
    "user_mobile":"1234567890",
    "user_nicename": "abc",
    "user_email": "[email protected]",
    "user_status": 0

  },
  {

    "user_pass": "$P$BwfQRzajR6R9eLeZbPnTgfQfsfPDhK0",
    "user_mobile":"0987654321",
    "user_nicename": "cdv",
    "user_email": "[email protected]",
    "user_status": 0
  },
  {

    "user_pass": "$P$Be15Zwqze.9OxoYfTOMd0WjlgXO7xe.",
    "user_mobile":"5432167890",
    "user_nicename": "zxc",
    "user_email": "[email protected]",
    "user_status": 0
  }
 ]

const newData = data.map(user => 
  Object.assign({}, user, {
    user_pass: user.user_mobile
  }))

console.log(data)
console.log(newData)
console.log(data === newData)
console.log(data[0] === newData[0])
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>

Comments

0
for (var i in users) { users[i].user_pass= users[i].user_mobile; }

Comments

-1

Here you go

var obj = [
  {

    "user_pass": "$PBa5$.cxL91nBU5cG4gqhNp8mWZoJgFY/",
    "user_mobile":"1234567890",
    "user_nicename": "abc",
    "user_email": "[email protected]",
    "user_status": 0

  },
  {

    "user_pass": "$P$BwfQRzajR6R9eLeZbPnTgfQfsfPDhK0",
    "user_mobile":"0987654321",
    "user_nicename": "cdv",
    "user_email": "[email protected]",
    "user_status": 0
  },
  {

    "user_pass": "$P$Be15Zwqze.9OxoYfTOMd0WjlgXO7xe.",
    "user_mobile":"5432167890",
    "user_nicename": "zxc",
    "user_email": "[email protected]",
    "user_status": 0
  }
 ]

for(var i =0 ; i < obj.length; i++){
     obj[i].user_pass= obj[i].user_mobile
}

console.log(obj);

5 Comments

I still don't understand why we use for loops on arrays/collections these days, when we have tons of high order functions implemented in Array.prototype
stick to your roots
That's why we don't evolve, and that's why we can't move to better programming. When people think like this. I dare you to stick to your roots for the next ten years and I guarantee you will become a dinosaur
Lol relax... For loop gives you more flexibility...