This question already has an answer here:
这个问题在这里已有答案:
- How to change value of object which is inside an array using JavaScript or jQuery? 13 answers
- 如何使用JavaScript或jQuery更改数组内的对象的值? 13个答案
[
{
"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
}
]
Above is my data.
以上是我的数据。
1 . I want to delete user_pass field in every object.
1。我想删除每个对象中的user_pass字段。
2 . I want to use user_mobile as user_pass
2。我想将user_mobile用作user_pass
my expectation result:
我的期望结果:
below i have mentioned my final result
下面我提到了我的最终结果
[
{
"user_pass": "1234567890",
"user_mobile":"1234567890",
"user_nicename": "abc",
"user_email": "abc@gmail.com",
"user_status": 0
},
{
"user_pass": "0987654321",
"user_mobile":"0987654321",
"user_nicename": "cdv",
"user_email": "cdv@gmail.com",
"user_status": 0
},
{
"user_pass": "5432167890",
"user_mobile":"5432167890",
"user_nicename": "zxc",
"user_email": "zxc@gmail.com",
"user_status": 0
}
]
any one help me out i am new to this technology .
任何人帮助我,我是这项技术的新手。
6 个解决方案
#1
1
You have to loop through the object array and reference user_mobile
to user_pass
.
您必须遍历对象数组并将user_mobile引用到user_pass。
var arr = [...data...];
arr.forEach(function(obj) {
obj.user_pass = obj.user_mobile;
});
#2
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);
#3
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);
#4
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": "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>
#5
0
for (var i in users) { users[i].user_pass= users[i].user_mobile; }
#6
-1
Here you go
干得好
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);
#1
1
You have to loop through the object array and reference user_mobile
to user_pass
.
您必须遍历对象数组并将user_mobile引用到user_pass。
var arr = [...data...];
arr.forEach(function(obj) {
obj.user_pass = obj.user_mobile;
});
#2
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);
#3
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);
#4
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": "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>
#5
0
for (var i in users) { users[i].user_pass= users[i].user_mobile; }
#6
-1
Here you go
干得好
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);