简体   繁体   English

查找类似的值并将项目附加到数组(javascript)

[英]Finding like values and appending items to array (javascript)

I have two arrays I'm trying to combine in a very specific way and I need a little guidance.我有两个 arrays 我正在尝试以一种非常具体的方式组合,我需要一些指导。 Array1 is an array of dates 30-40 items, Array 2 is a list of objects with a date inside one of the attributes. Array1 是一个包含 30-40 个项目的日期数组,Array 2 是一个对象列表,其中一个属性中有一个日期。 I'm trying to append the object in array 2 to the index of array1 when the dates match.当日期匹配时,我正在尝试将数组 2 中的 append 和 object 转换为数组 1 的索引。

I want to put arr2 in the same index as arr1 if the dates match.如果日期匹配,我想将 arr2 放在与 arr1 相同的索引中。

 const arr = [ "2022-06-26T07:00:00.000Z", "2022-06-27T07:00:00.000Z", "2022-06-28T07:00:00.000Z", "2022-06-29T07:00:00.000Z", "2022-06-30T07:00:00.000Z", "2022-07-01T07:00:00.000Z", "2022-07-02T07:00:00.000Z", "2022-07-03T07:00:00.000Z", "2022-07-04T07:00:00.000Z", "2022-07-05T07:00:00.000Z", "2022-07-06T07:00:00.000Z", "2022-07-07T07:00:00.000Z", "2022-07-08T07:00:00.000Z", "2022-07-09T07:00:00.000Z", "2022-07-10T07:00:00.000Z", "2022-07-11T07:00:00.000Z", "2022-07-12T07:00:00.000Z", "2022-07-13T07:00:00.000Z", "2022-07-14T07:00:00.000Z", "2022-07-15T07:00:00.000Z", "2022-07-16T07:00:00.000Z", "2022-07-17T07:00:00.000Z", "2022-07-18T07:00:00.000Z", "2022-07-19T07:00:00.000Z", "2022-07-20T07:00:00.000Z", "2022-07-21T07:00:00.000Z", "2022-07-22T07:00:00.000Z", "2022-07-23T07:00:00.000Z", "2022-07-24T07:00:00.000Z", "2022-07-25T07:00:00.000Z", "2022-07-26T07:00:00.000Z", "2022-07-27T07:00:00.000Z", "2022-07-28T07:00:00.000Z", "2022-07-29T07:00:00.000Z", "2022-07-30T07:00:00.000Z", "2022-07-31T07:00:00.000Z", "2022-08-01T07:00:00.000Z", "2022-08-02T07:00:00.000Z", "2022-08-03T07:00:00.000Z", "2022-08-04T07:00:00.000Z", "2022-08-05T07:00:00.000Z", "2022-08-06T07:00:00.000Z" ] const arr2 = [ { "gsi1SK": "name ", "searchPK": "thing", "SK": "uuid", "Desc": "place #1205", "PK": "thing uuid", "searchSK": "7/1/2022", "gsi1PK": "thing", "Complete": false, "Users": [ "person1", "person2" ] }, { "gsi1SK": "name", "searchPK": "thing", "SK": "uuid", "Desc": "place#124124", "PK": "thing uuid", "searchSK": "7/4/2022", "gsi1PK": "thing", "Complete": false, "Users": [ "person2", "person45" ] } ] console.log([arr, arr2]);

Plan计划

You've got two obvious options:你有两个明显的选择:

  • A. Look at each of the objects, finding a home for each one in turn A. 看每一件物品,依次为每件物品找一个家
  • B. Look at each of the dates, collecting all the objects that belong to it B.查看每个日期,收集所有属于它的对象

Which method makes more sense for you will depend on other factors you haven't covered in your post.哪种方法对您更有意义将取决于您在帖子中未涵盖的其他因素。 I think the main question is: is it guaranteed that the date list will contain a proper home for every object?我认为主要问题是:是否保证日期列表将包含每个 object 的正确归宿? If no, then do you want to drop the objects without proper homes, or do you want to create a proper home for the objects如果不是,那么您是要丢弃没有合适家的对象,还是要为对象创建合适的家

Performance can also matter, but really only if you expect either list to be very long or if you need to run this process multiple times (such as in a React component in the browser).性能也很重要,但实际上只有当您希望任一列表非常长或者您需要多次运行此过程时(例如在浏览器中的 React 组件中)。

Implement实施

Loop through the list you chose.循环浏览您选择的列表。 For each item, scan the other list for the relevant item(s): its home or its children.对于每个项目,扫描另一个列表以查找相关项目:它的家或它的孩子。 Take the appropriate action for those items depending on which plan you chose.根据您选择的计划对这些项目采取适当的措施。

Another consideration is: don't mutate your arguments.另一个考虑因素是:不要改变您的 arguments。 That means you probably need to create copies of the two input arrays before you do the work.这意味着您可能需要在执行工作之前创建两个输入 arrays 的副本。 If the arrays contain objects rather than scalars, you can't just do array.slice() to create a copy.如果 arrays 包含对象而不是标量,则不能仅执行array.slice()来创建副本。

  • For an array of POJOs, you can convert the source to a string and then back again to create a clone.对于 POJO 数组,您可以将源转换为字符串,然后再返回以创建克隆。
  • The array of dates will need special handling, because JSON.parse will not revive dates.日期数组需要特殊处理,因为JSON.parse不会恢复日期。

Mutating arguments is generally a bad practice, at least in the functional paradigm that underlies many popular front-end frameworks today.突变 arguments 通常是一种不好的做法,至少在当今许多流行前端框架的功能范式中是这样。 Plus, if you create your own copies of the input data, you can gain efficiency by moving items from the source arrays to the output array, which means that subsequent iterations won't have to re-examine items that have already been processed.此外,如果您创建自己的输入数据副本,您可以通过项目从源 arrays 移动到 output 数组来提高效率,这意味着后续迭代不必重新检查已处理的项目。

You seem to have a handle on the date conversion part.您似乎掌握了日期转换部分。 Here I've defined two short sample arrays to represent arr2 and newArr.在这里,我定义了两个简短的示例 arrays 来表示 arr2 和 newArr。 Then, a map function to create the output.然后,一个 map function 创建 output。

 const arr2 = [ { "OTHER_FIELDS": "TOP SECRET", "searchSK":"7/4/2022" }, { "OTHER_FIELDS": "TOP SECRET", "searchSK":"7/9/2022" } ]; const newArr = [ [ "7/2/2022" ], [ "7/3/2022" ], [ "7/4/2022" ], [ "7/5/2022" ], [ "7/6/2022" ], [ "7/7/2022" ], [ "7/8/2022" ], [ "7/9/2022" ], [ "7/10/2022" ] ]; // for each subarray in newArr, return an array containing the existing element plus any elements from arr2 found by the filter function const output = newArr.map(el => [...el, ...arr2.filter(a2 => a2.searchSK === el[0])]); console.log(output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM