您好我想在Javascript中执行此操作:S有人可以帮助我吗?
$acounts = Array (
'Amount' => 5,
'InvNum' => '523',
'TipAmount' => 0.90
);
$defFileds = Array ('Amount','InvNum');
foreach ($acounts as $field => $val) {
if (!in_array($field, $defFileds)) {
$acounts['ExtData'] .= "<{$field}>'" . $val . "</{$field}>";
unset ($acounts[$field]);
}
}
此代码验证这些数组是否具有相同,如果不相同,则将添加和属性。这段代码很实用。
但我在javascript上需要这个,我不知道怎么能这样做。
外观: var opt = { firstName:&#34; John&#34;, lastName:&#34; Doe&#34;, 年龄:50岁, eyeColor:&#34;蓝色&#34; };
var list = [
"firstName",
"lastName",
"age"
];
opt["ExtData"]="";
//document.write(opt.ExtData);
list.forEach(function(l) {
for (var p in opt){
if(p == l){
document.write(p);
opt["ExtData"]="<"+p+">" +p.value+ "</"+p">";
}
}
});
</script>
答案 0 :(得分:0)
<script>
var opt = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
var list = [
"firstName",
"lastName",
"age"
];
opt["ExtData"]="";
//document.write(opt.ExtData);
list.forEach(function(l) {
for (var p in opt){
if(p == l){
document.write(p);
opt["ExtData"] += "<"+p+">"+opt[p]+"<"+p+">";
}
}
});
for(var p in opt){
alert(opt[p]);
}
</script>