如何使用JavaScript中的.map()方法按顺序更新对象数组与数组和参数一起?

huangapple go评论68阅读模式
英文:

How can I update an object array in sequence with an array for and argument with .map() in JavaScript?

问题

Here is the translated code portion:

我是一名初学者为了练习理解 `.map()`我正在尝试编写一个回调函数该函数返回一个更新后的全名列表例如将 Jim 的姓氏返回为 Smith依此类推`employees[0].familyName = lastNames[0]``employees[1].familyName = lastNames[1]` 等等...

const lastNames = ["Smith", "Anderson"];

const employees = [
  {
    name: "Jim",
    familyName: "",
  },
  {
    name: "Jill",
    familyName: "",
  }
];

这是我尝试的内容期望它返回一个名为 `fullNames` 的新对象数组其中包含以下键/值对但我可能在匹配索引号方面走错了方向

function lastNameUpdater(arr, objArr) {
  let matchNumber = arr.findIndex() === objArr.findIndex();
  const fullNames = objArr.map((element) => {
    return matchNumber;
    if (matchNumber) {
      return objArr.key = arr;
    }
    return fullNames;
  })
}

lastNameUpdater(lastNames, employees);

如果您需要进一步的帮助或解释,请告诉我。

英文:

I'm a beginner-level student and for practice in understanding .map(), I'm trying to write a callback function that returns an updated list of full names that would return Jim's last name as Smith etc. employees[0].familyName = lastNames[0], employees[1].familyName = lastNames[1] and so on....

const lastNames = ["Smith", "Anderson"];

const employees = [
  {
    name: "Jim",
    familyName:"",
  },
  {
    name: "Jill",
    familyName: "",
    
  }
];

This is what I've tried expecting it to return a new object array called fullNames with the following key/value pairs but am lost in the wrong direction possibly with matching the index numbers:

function lastNameUpdater(arr, objArr) {
  let matchNumber = arr.findIndex() === objArr.findIndex();
  const fullNames = objArr.map((element) => {
    return matchNumber;
    if (matchNumber) {
      return objArr.key = arr;
    }
    return fullNames;
  })
}

lastNameUpdater(lastNames, employees);

答案1

得分: 3

请使用传递给回调函数的第二个 index 参数,然后您可以使用 spread notation 来覆盖 familyName 属性。因为索引相匹配,这将将每个姓氏与正确的员工配对:

function update(employees, lastNames) {
  return employees.map(function(employee, index) {
    return {...employee, familyName: lastNames[index]}
  });
}

const lastNames = ["Smith", "Anderson"];
const employees = [
  {
    name: "Jim",
    familyName:"",
  },
  {
    name: "Jill",
    familyName: "",
  }
];

console.log(update(employees, lastNames))
英文:

Just use the second index parameter that's passed to the callback, then you can use spread notation to overwrite the familyName attribute. Because the indices match up, this will pair each family name with the correct employee:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function update(employees, lastNames) {
  return employees.map(function(employee, index) {
    return {...employee, familyName: lastNames[index]}
  });
}

const lastNames = [&quot;Smith&quot;, &quot;Anderson&quot;];
const employees = [
  {
    name: &quot;Jim&quot;,
    familyName:&quot;&quot;,
  },
  {
    name: &quot;Jill&quot;,
    familyName: &quot;&quot;,
    
  }
];

console.log(update(employees, lastNames))

<!-- end snippet -->

答案2

得分: 1

你可以使用给定给 map 回调的索引参数来访问另一个数组中对应的姓氏。

const lastNames = ["Smith", "Anderson"];
const employees = [
  {
    name: "Jim",
    familyName:"",
  },
  {
    name: "Jill",
    familyName: "",
  }
];
const res = employees.map((o, i) => ({...o, familyName: lastNames[i]}));
console.log(res);
英文:

You can use the index argument given to the map callback to access the corresponding last name from the other array.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const lastNames = [&quot;Smith&quot;, &quot;Anderson&quot;];
const employees = [
  {
    name: &quot;Jim&quot;,
    familyName:&quot;&quot;,
  },
  {
    name: &quot;Jill&quot;,
    familyName: &quot;&quot;,
  }
];
const res = employees.map((o, i) =&gt; ({...o, familyName: lastNames[i]}));
console.log(res);

<!-- end snippet -->

答案3

得分: 1

你正在误用findIndex;实际上,你甚至不需要它。你可以在这里了解更多信息。

你真正需要的是使用map的第二个参数,它是数组中当前元素的index,然后在lastNames数组中选择相同索引的元素。

const lastNames = ['Smith', 'Anderson'];

const employees = [
  {
    name: 'Jim',
    familyName: '',
  },
  {
    name: 'Jill',
    familyName: '',
  },
];

function lastNameUpdater(arr, objArr) {
  const fullNames = objArr.map((element, index) => ({
    ...element,
    familyName: arr[index],
  }));
  return fullNames;
}

// [
//     { name: 'Jim', familyName: 'Smith' },
//     { name: 'Jill', familyName: 'Anderson' }
// ]
console.log(lastNameUpdater(lastNames, employees));
英文:

You are misusing findIndex; actually, you don't even need it. You can read more about it here

What you really need is to use the map's second argument, which is the index of the current element in the array, and pick the element under the same index in the lastNames array.

const lastNames = [&#39;Smith&#39;, &#39;Anderson&#39;];

const employees = [
  {
    name: &#39;Jim&#39;,
    familyName: &#39;&#39;,
  },
  {
    name: &#39;Jill&#39;,
    familyName: &#39;&#39;,
  },
];

function lastNameUpdater(arr, objArr) {
  const fullNames = objArr.map((element, index) =&gt; ({
    ...element,
    familyName: arr[index],
  }));
  return fullNames;
}

// [
//     { name: &#39;Jim&#39;, familyName: &#39;Smith&#39; },
//     { name: &#39;Jill&#39;, familyName: &#39;Anderson&#39; }
// ]
console.log(lastNameUpdater(lastNames, employees));

huangapple
  • 本文由 发表于 2023年5月13日 23:59:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定