Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 比较两个键值对象的值_Javascript_Json_For Loop_Foreach_Key Value - Fatal编程技术网

Javascript 比较两个键值对象的值

Javascript 比较两个键值对象的值,javascript,json,for-loop,foreach,key-value,Javascript,Json,For Loop,Foreach,Key Value,我有两个对象(LABELS1和LABELS2)要循环,如果LABELS1中的任何ID与LABEL2中的任何ID匹配,那么我想用LABELS2中的simple_值重新指定LABELS1的simple_值。但是,每当我比较这些值时,没有任何匹配项。这是我在下面尝试过的。任何帮助都将不胜感激 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/

我有两个对象(LABELS1和LABELS2)要循环,如果LABELS1中的任何ID与LABEL2中的任何ID匹配,那么我想用LABELS2中的simple_值重新指定LABELS1的simple_值。但是,每当我比较这些值时,没有任何匹配项。这是我在下面尝试过的。任何帮助都将不胜感激

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">

    const LABELS1 = [
{"id":"bread", "simple_value":"Bread"},
{"id":"apple", "simple_value":"Apple"}
];
    const LABELS2 = [
{"id":"bread", "simple_value":"Bread with Butter", "detailed_value":"Toasted Bread with a Dab of Butter"},
{"id":"wine", "simple_value":"Wine", "detailed_value":"Wine with Cheese"}
];
    var labels1= [];
    var labels2= [];
        
    $.when(
    $.getJSON(LABELS1, json => {
       labels1= json;
    }), 
    $.getJSON(LABELS2, json => {       
      labels2= json; 
    })
    ).then(() => {
      Object.keys(labels1).forEach(key => {
         if (labels2[key].id=== labels1[key].id) {
            labels1[key].simple_value= labels2[key].simple_value;
         }
     });      
    });

</script>
</body>
</html>

常数标签1=[
{“id”:“bread”,“simple_值”:“bread”},
{“id”:“apple”,“simple_值”:“apple”}
];
常数标签2=[
{“id”:“面包”、“简单价值”:“黄油面包”、“详细价值”:“黄油烤面包”},
{“id”:“葡萄酒”、“简单价值”:“葡萄酒”、“详细价值”:“奶酪葡萄酒”}
];
var标签1=[];
var标签2=[];
美元。什么时候(
$.getJSON(标签1,json=>{
labels1=json;
}), 
$.getJSON(标签2,json=>{
labels2=json;
})
).然后(()=>{
Object.keys(labels1).forEach(key=>{
if(labels2[key].id==labels1[key].id){
labels1[key]。简单值=labels2[key]。简单值;
}
});      
});
对象。键(标签1)
将返回项索引数组,而不是id,因为
标签1
标签2
是数组。必须循环抛出其中一个数组的所有项,并尝试在第二个数组中找到匹配项

const LABELS1=[
{“id”:“bread”,“simple_值”:“bread”},
{“id”:“apple”,“simple_值”:“apple”}
];
常数标签2=[
{“id”:“面包”、“简单价值”:“黄油面包”、“详细价值”:“黄油烤面包”},
{“id”:“葡萄酒”、“简单价值”:“葡萄酒”、“详细价值”:“奶酪葡萄酒”}
];
var labels1=labels1;
var labels2=labels2;
用于(标签1的常量标签1){
常量label2Index=labels2.findIndex(label2=>label2.id==label1.id)
如果(label2Index!=-1){
label1.simple_值=labels2[label2Index]。simple_值
}
}
console.log(标签1)
对象。键(标签1)
将返回项索引数组,而不是id,因为
标签1
标签2
是数组。必须循环抛出其中一个数组的所有项,并尝试在第二个数组中找到匹配项

const LABELS1=[
{“id”:“bread”,“simple_值”:“bread”},
{“id”:“apple”,“simple_值”:“apple”}
];
常数标签2=[
{“id”:“面包”、“简单价值”:“黄油面包”、“详细价值”:“黄油烤面包”},
{“id”:“葡萄酒”、“简单价值”:“葡萄酒”、“详细价值”:“奶酪葡萄酒”}
];
var labels1=labels1;
var labels2=labels2;
用于(标签1的常量标签1){
常量label2Index=labels2.findIndex(label2=>label2.id==label1.id)
如果(label2Index!=-1){
label1.simple_值=labels2[label2Index]。simple_值
}
}
console.log(标签1)

您可以将标签(#2)缓存到其索引中,然后在将标签(#1)映射到其
简单_值时,通过其索引检索标签(#2)

const
标签_1=[
{“id”:“bread”,“simple_值”:“bread”},
{“id”:“apple”,“simple_值”:“apple”}
],
标签_2=[
{“id”:“面包”,“简单价值”:“黄油面包”,
“详细价值”:“涂有少量黄油的烤面包”},
{“id”:“wine”,“simple_value”:“wine”,
“详细价值”:“奶酪葡萄酒”}
];
//将标签(#2)缓存到其索引中
const idToIndex=LABELS_2.reduce((acc,{id},index)=>
({…acc[id]:index}),{});
const labels=labels_1.map({id,simple_value})=>({
id,简单值:((索引)=>
标签2[索引]?.简单值| |简单值)
(idToIndex[id])
}));
控制台日志(标签)

.as console wrapper{top:0;max height:100%!important;}
您可以将标签(#2)缓存到它们的索引中,然后通过索引检索标签(#2),同时将标签(#1)映射到它们的
简单_值

const
标签_1=[
{“id”:“bread”,“simple_值”:“bread”},
{“id”:“apple”,“simple_值”:“apple”}
],
标签_2=[
{“id”:“面包”,“简单价值”:“黄油面包”,
“详细价值”:“涂有少量黄油的烤面包”},
{“id”:“wine”,“simple_value”:“wine”,
“详细价值”:“奶酪葡萄酒”}
];
//将标签(#2)缓存到其索引中
const idToIndex=LABELS_2.reduce((acc,{id},index)=>
({…acc[id]:index}),{});
const labels=labels_1.map({id,simple_value})=>({
id,简单值:((索引)=>
标签2[索引]?.简单值| |简单值)
(idToIndex[id])
}));
控制台日志(标签)

。作为控制台包装{top:0;最大高度:100%!important;}
这就像一个魅力!非常感谢你。我今天过得很开心。这真是个奇迹!非常感谢你。让我开心。那是另一种方式!谢谢,这是另一种方式!非常感谢。