我正在尝试更新对象属性:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="container-fluid">
<div class="row full-height">
<div class="col-md-5 MainBackgroundImages leftHalf"></div>
<div class="col-md-2 centerHalf">
<div class="navbar navbar-default" id="custom-bootstrap-menu" role="navigation">
<div class="navbar-header">
<a class="navbar-brand" href="#">Ishka Sport</a>
<button class="navbar-toggle" data-target=".navbar-menubuilder" data-toggle="collapse" type="button">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse navbar-menubuilder">
<ul class="nav navbar-nav navbar-left">
<li><a href="/">Home</a></li>
<li><a href="/products">kayak</a></li>
<li><a href="/about-us">Nipper Boards</a></li>
<li><a href="/contact">Accessories</a></li>
</ul>
</div>
</div>
</div>
<div class="col-md-5 MainBackgroundImages rightHalf"></div>
</div>
</div>
新对象将如下更新。
data : [
{"id":1,"rp":"2426","cr":"11-11"},
{"id":1,"rp":"1119","cr":"19-21"},
{"id":1,"rp":"3453","cr":"23-81"}
]
我希望更新所有值的对象cr属性,例如使用javascript.replace()方法,我会执行replace(“-”,“ /”)。
答案 0 :(得分:2)
这只是.as-console-wrapper { max-height: 100% !important; top: 0; }
和Array#map
的简单用法:
ES2018
String#replace
答案 1 :(得分:1)
您可以迭代并映射数组。
for (var y = 0; y < x[i].name.length; y++) {
table.innerHTML += '<th>' + x[storeFor].name[y] + '</th>';
}
// remove the for loop here; not sure why you are checking the length here; the age is a number and hence .length property will not work for number
table2.innerHTML += '<th>' + x[storeFor].age + '</th>';
for (var o = 0; o < x[i].intrests.length; o++) {
table3.innerHTML += '<th>' + x[storeFor].intrests[o] + '</th>';
}
var data = [{ id: 1, rp: "2426", cr: "11-11" }, { id: 1, rp: "1119", cr: "19-21" }, { id: 1, rp: "3453", cr: "23-81" }],
newArray = data.map(o => Object.assign({}, o, { cr: o.cr.replace("-","/") }));
console.log(newArray);
答案 2 :(得分:1)
您可以使用const data = [{"id":1,"rp":"2426","cr":"11-11"},{"id":1,"rp":"1119","cr":"19-21"},{"id":1,"rp":"3453","cr":"23-81"}]
const r = data.map(({ cr, ...rest }) => ({ cr: cr.replace('-', '/'), ...rest }));
console.log(r);
方法创建新数组,并使用map
更新replace
属性。
cr
答案 3 :(得分:0)
尝试一下:
const data = [
{"id":1,"rp":"2426","cr":"11-11"},
{"id":1,"rp":"1119","cr":"19-21"},
{"id":1,"rp":"3453","cr":"23-81"}
];
const newData = data.map(item => ({...item, cr: item.cr.replace(/-/g, '/')}));
console.log(newData);
如果您需要它在IE11中工作:
const data = [
{"id":1,"rp":"2426","cr":"11-11"},
{"id":1,"rp":"1119","cr":"19-21"},
{"id":1,"rp":"3453","cr":"23-81"}
];
const newData = data.map(
function(item) {
return {
id: item.id,
rp: item.rp,
cr: item.cr.replace(/-/g, '/')
}
}
);
console.log(newData);
答案 4 :(得分:0)
您可以尝试
let data = [
{"id":1,"rp":"2426","cr":"11-11"},
{"id":1,"rp":"1119","cr":"19-21"},
{"id":1,"rp":"3453","cr":"23-81"}
];
let newData = data.map(e=>{
e.cr = e.cr.replace(/-/, '/')
return e
})
console.log(newData)
答案 5 :(得分:0)
只需尝试以下
data.forEach(function(object){
object["cr"] = object["cr"].replace("-","/");
});
答案 6 :(得分:0)
const data = [{"id":1,"rp":"2426","cr":"11-11"},{"id":1,"rp":"1119","cr":"19-21"},{"id":1,"rp":"3453","cr":"23-81"}]
const r = JSON.parse(JSON.stringify(data).replace(/-/g,'/'))
console.log(r);