如何使用javascript删除json数组中的特定字段?

时间:2016-11-30 05:56:18

标签: javascript json node.js

List

以上是我的数据。

1。我想删除每个对象中的user_pass字段。

2。我想将user_mobile用作user_pass

我的期望结果:

下面我提到了我的最终结果

[
  {

    "user_pass": "$PBa5$.cxL91nBU5cG4gqhNp8mWZoJgFY/",
    "user_mobile":"1234567890",
    "user_nicename": "abc",
    "user_email": "abc@gmail.com",
    "user_status": 0

  },
  {

    "user_pass": "$P$BwfQRzajR6R9eLeZbPnTgfQfsfPDhK0",
    "user_mobile":"0987654321",
    "user_nicename": "cdv",
    "user_email": "cdv@gmail.com",
    "user_status": 0
  },
  {

    "user_pass": "$P$Be15Zwqze.9OxoYfTOMd0WjlgXO7xe.",
    "user_mobile":"5432167890",
    "user_nicename": "zxc",
    "user_email": "zxc@gmail.com",
    "user_status": 0
  }
 ]

任何人帮助我,我是这项技术的新手。

6 个答案:

答案 0 :(得分:1)

您必须遍历对象数组并将cl.exe引用到user_mobile

user_pass

答案 1 :(得分:0)

forEach

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

答案 2 :(得分:0)



var data = [{

  "user_pass": "$PBa5$.cxL91nBU5cG4gqhNp8mWZoJgFY/",
  "user_mobile": "1234567890",
  "user_nicename": "abc",
  "user_email": "abc@gmail.com",
  "user_status": 0

}, {

  "user_pass": "$P$BwfQRzajR6R9eLeZbPnTgfQfsfPDhK0",
  "user_mobile": "0987654321",
  "user_nicename": "cdv",
  "user_email": "cdv@gmail.com",
  "user_status": 0
}, {

  "user_pass": "$P$Be15Zwqze.9OxoYfTOMd0WjlgXO7xe.",
  "user_mobile": "5432167890",
  "user_nicename": "zxc",
  "user_email": "zxc@gmail.com",
  "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);




答案 3 :(得分:0)

如果需要以不可变的方式进行,可以映射数组并将所有对象属性分配给新对象。



const data = [
  {

    "user_pass": "$PBa5$.cxL91nBU5cG4gqhNp8mWZoJgFY/",
    "user_mobile":"1234567890",
    "user_nicename": "abc",
    "user_email": "abc@gmail.com",
    "user_status": 0

  },
  {

    "user_pass": "$P$BwfQRzajR6R9eLeZbPnTgfQfsfPDhK0",
    "user_mobile":"0987654321",
    "user_nicename": "cdv",
    "user_email": "cdv@gmail.com",
    "user_status": 0
  },
  {

    "user_pass": "$P$Be15Zwqze.9OxoYfTOMd0WjlgXO7xe.",
    "user_mobile":"5432167890",
    "user_nicename": "zxc",
    "user_email": "zxc@gmail.com",
    "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>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

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

答案 5 :(得分:-1)

你去吧

&#13;
&#13;
var obj = [
  {

    "user_pass": "$PBa5$.cxL91nBU5cG4gqhNp8mWZoJgFY/",
    "user_mobile":"1234567890",
    "user_nicename": "abc",
    "user_email": "abc@gmail.com",
    "user_status": 0

  },
  {

    "user_pass": "$P$BwfQRzajR6R9eLeZbPnTgfQfsfPDhK0",
    "user_mobile":"0987654321",
    "user_nicename": "cdv",
    "user_email": "cdv@gmail.com",
    "user_status": 0
  },
  {

    "user_pass": "$P$Be15Zwqze.9OxoYfTOMd0WjlgXO7xe.",
    "user_mobile":"5432167890",
    "user_nicename": "zxc",
    "user_email": "zxc@gmail.com",
    "user_status": 0
  }
 ]

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

console.log(obj);
&#13;
&#13;
&#13;