Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.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在map函数中比较两个数组_Javascript - Fatal编程技术网

Javascript在map函数中比较两个数组

Javascript在map函数中比较两个数组,javascript,Javascript,我有两个数组,我试着列一个列表 约翰、加布有什么水果?列表xD John和Gaby各自有自己的id,并将这两个id与数组Fruits进行比较。当fruitId与John或Gaby's Juice匹配时,它是John或Gaby's水果 约翰:苹果,奥朗格 加比:芒果 Fruits = [ {fruitId: 'abc', name: 'apple'}, {fruitId: 'abc', name: 'orange'}, {fruitId: 'def', name: 'mango'},

我有两个数组,我试着列一个列表

约翰、加布有什么水果?列表xD

John和Gaby各自有自己的id,并将这两个id与数组Fruits进行比较。当fruitId与John或Gaby's Juice匹配时,它是John或Gaby's水果

约翰:苹果,奥朗格

加比:芒果

Fruits = [
  {fruitId: 'abc', name: 'apple'},
  {fruitId: 'abc', name: 'orange'},
  {fruitId: 'def', name: 'mango'},
  {fruitId: 'egt', name: 'pineapple'}
]

Juices = [
  {juiceId: 'abc', customer: 'John'},
  {juiceId: 'def', customer: 'Gaby'}
]
我想应该是这样的

array.map((list) => {
   <div>// customer's name {list.customer}</div>
   <div>// fruit's name {list.name}</div>
})
array.map((列表)=>{
//客户名称{list.customer}
//水果名称{list.name}
})

对于这个场景,这里有一个非常简单且易于理解的方法。实现这一目标可能有一些简单的方法,但即使使用高级javascript方法,也非常可读,不需要高级javascript方法

  const Fruits = [
    {fruitId: 'abc', name: 'apple'},
    {fruitId: 'abc', name: 'orange'},
    {fruitId: 'def', name: 'mango'},
    {fruitId: 'egt', name: 'pineapple'}
  ]

  const Juices = [
    {juiceId: 'abc', customer: 'John'},
    {juiceId: 'def', customer: 'Gaby'}
  ]

  let finalArray = []
  Juices.map((juice) => {
    Fruits.map((fruit) => {
      if(juice.juiceId === fruit.fruitId){
        finalArray.push({customer: juice.customer, fruit: fruit.name}) 
      }
    });
  });

  console.log(finalArray)

对于这个场景,这里有一个非常简单且可以理解的方法。实现这一目标可能有一些简单的方法,但即使使用高级javascript方法,也非常可读,不需要高级javascript方法

  const Fruits = [
    {fruitId: 'abc', name: 'apple'},
    {fruitId: 'abc', name: 'orange'},
    {fruitId: 'def', name: 'mango'},
    {fruitId: 'egt', name: 'pineapple'}
  ]

  const Juices = [
    {juiceId: 'abc', customer: 'John'},
    {juiceId: 'def', customer: 'Gaby'}
  ]

  let finalArray = []
  Juices.map((juice) => {
    Fruits.map((fruit) => {
      if(juice.juiceId === fruit.fruitId){
        finalArray.push({customer: juice.customer, fruit: fruit.name}) 
      }
    });
  });

  console.log(finalArray)

你的问题不太清楚,但这是我从你的提问中所能理解的最好的

函数findFruits(){
风险值=[
{FROUTID:'abc',name:'apple'},
{FROUTID:'abc',name:'orange'},
{FROUTID:'def',name:'mango'},
{FROUTID:'egt',名称:'菠萝'}
];
var果汁=[
{juiceId:'abc',客户:'John'},
{juiceId:'def',客户:'Gaby'}
];
var target=document.getElementById('target');
var div=document.createElement('div');
果汁。forEach(果汁=>{
var p=document.createElement('p');
var string=`${juice.customer}喜欢`;
var matches=fruits.filter(fruit=>fruit.fruitId==juice.juiceId);
matches.forEach(match=>string+=`${match.name}`);
p、 innerHTML=字符串;
儿童组(p);
});
目标儿童(div);
}
findFruits()

你的问题不太清楚,但这是我从你的提问中所能理解的最好的问题

函数findFruits(){
风险值=[
{FROUTID:'abc',name:'apple'},
{FROUTID:'abc',name:'orange'},
{FROUTID:'def',name:'mango'},
{FROUTID:'egt',名称:'菠萝'}
];
var果汁=[
{juiceId:'abc',客户:'John'},
{juiceId:'def',客户:'Gaby'}
];
var target=document.getElementById('target');
var div=document.createElement('div');
果汁。forEach(果汁=>{
var p=document.createElement('p');
var string=`${juice.customer}喜欢`;
var matches=fruits.filter(fruit=>fruit.fruitId==juice.juiceId);
matches.forEach(match=>string+=`${match.name}`);
p、 innerHTML=字符串;
儿童组(p);
});
目标儿童(div);
}
findFruits()

如果我理解你的问题,你可以
减少果汁和
过滤
水果

const Fruits=[{fruitId:'abc',name:'apple'},{fruitId:'abc',name:'orange'},{fruitId:'def',name:'mango'},{fruitId:'egt',name:'菠萝'}]
const Juices=[{juiceId:'abc',customer:'John'},{juiceId:'def',customer:'Gaby'}]
const r=Juices.reduce((r,{customer,juiceId})=>{
r[customer]=Fruits.filter(y=>y.fruitId==juiceId).map(x=>x.name).join(','))
返回r
}, {})
console.log(r)
console.log('John:',r.John)

console.log('Gaby:',r.Gaby)
如果我理解你的问题,你可以
减少果汁和
过滤
水果

const Fruits=[{fruitId:'abc',name:'apple'},{fruitId:'abc',name:'orange'},{fruitId:'def',name:'mango'},{fruitId:'egt',name:'菠萝'}]
const Juices=[{juiceId:'abc',customer:'John'},{juiceId:'def',customer:'Gaby'}]
const r=Juices.reduce((r,{customer,juiceId})=>{
r[customer]=Fruits.filter(y=>y.fruitId==juiceId).map(x=>x.name).join(','))
返回r
}, {})
console.log(r)
console.log('John:',r.John)

log('Gaby:',r.Gaby)
您首先需要的是通过
水果ID
快速引用水果。将一组ID添加到一组水果名称中就非常适合了

要创建此文件,请使用

然后您可以将
榨汁
数组转换为更像您所需输出的内容

const array = Juices.map(({ juiceId, customer }) => ({
  customer,
  name: (fruitMap.get(juiceId) || []).join(', ')
}))
const-Fruits=[{“水果ID”:“abc”,“名称”:“苹果”},{“水果ID”:“abc”,“名称”:“橙色”},{“水果ID”:“def”,“名称”:“芒果”},{“水果ID”:“egt”,“名称”:“菠萝”}]
const juice=[{“juiceId”:“abc”,“customer”:“John”},{“juiceId”:“def”,“customer”:“Gaby”}]
const fruitMap=Fruits.reduce((map,{fruitId,name})=>{
让水果=地图。获取(水果ID)| |[]
水果。推(名称)
返回地图。集合(水果ID,水果)
},新映射())
const array=Juices.map({juiceId,customer})=>({
顾客
名称:(fruitMap.get(juiceId)| |[]).join(','))
}))

console.info(array)
您需要的第一件事是通过
水果ID
快速引用水果。将一组ID添加到一组水果名称中就非常适合了

要创建此文件,请使用

然后您可以将
榨汁
数组转换为更像您所需输出的内容

const array = Juices.map(({ juiceId, customer }) => ({
  customer,
  name: (fruitMap.get(juiceId) || []).join(', ')
}))
const-Fruits=[{“水果ID”:“abc”,“名称”:“苹果”},{“水果ID”:“abc”,“名称”:“橙色”},{“水果ID”:“def”,“名称”:“芒果”},{“水果ID”:“egt”,“名称”:“菠萝”}]
const juice=[{“juiceId”:“abc”,“customer”:“John”},{“juiceId”:“def”,“customer”:“Gaby”}]
const fruitMap=Fruits.reduce((map,{fruitId,name})=>{
让水果=地图。获取(水果ID)| |[]
水果。推(名称)
返回地图。集合(水果ID,水果)
},新映射())
const array=Juices.map({juiceId,customer})=>({
顾客
名称:(fruitMap.get(juiceId)| |[]).join(','))
}))

console.info(数组)
您尝试过什么吗?是否有某个特定部分您遇到了问题?如果您能向我们展示您希望最终输出的内容,这会有所帮助。apple和orange是否都有相同的
水果ID
?这种关系没有意义。为什么FruitId是客户的一个连接?客户查询被称为