简体   繁体   English

Javascript:如何将动态键插入到地图/对象中?

[英]Javascript: How to insert dynamic keys into map/objects?

I am a JS beginner and am doing basic tutorials. 我是JS初学者,正在做基础教程。 I am trying to perform a zip function to return a list of {videoID: bookmarkID}. 我正在尝试执行zip函数以返回{videoID:书签ID}的列表。 So take for example: 举个例子:

var videos = [
        {
            "id": 70111470,
            "title": "Die Hard"
        },
        {
            "id": 654356453,
            "title": "Bad Boys"
        },
        {
            "id": 65432445,
            "title": "The Chamber"
        }
    ],
    bookmarks = [
        {id: 470, time: 23432},
        {id: 453, time: 234324},
        {id: 445, time: 987834}
    ];
}

This does not work (I get unexpected token '.'): 这不起作用(我得到了意外的令牌“。”):

return Array.zip(videos,bookmarks, function(v, b){
  return {v.id: b.id};
});

This does, but returns a list containing {'v': bookmarkID}: 可以,但是返回包含{'v':bookmarkID}的列表:

return Array.zip(videos,bookmarks, function(v, b){
  return {v: b.id};
});

How do I get the video ID to be the key for the value bookmarkID? 如何获得视频ID作为值bookmarkID的键? Also, are these technically maps or objects? 而且,这些在技术上是地图还是对象? Thanks. 谢谢。

Try: 尝试:

return Array.zip(videos,bookmarks, function(v, b){
  return {[v.id]: b.id};
});

You could map one and get the elemnt of the other with the same index. 您可以映射一个索引并获得另一个具有相同索引的元素。

 var videos = [{ "id": 70111470, "title": "Die Hard" }, { "id": 654356453, "title": "Bad Boys" }, { "id": 65432445, "title": "The Chamber" }], bookmarks = [{ id: 470, time: 23432 }, { id: 453, time: 234324 }, { id: 445, time: 987834 }], zipped = videos.map(function (v, i) { var o = {}; o[v.id] = bookmarks[i].id; return o; }); console.log(zipped); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

ES6 ES6

 var videos = [{ "id": 70111470, "title": "Die Hard" }, { "id": 654356453, "title": "Bad Boys" }, { "id": 65432445, "title": "The Chamber" }], bookmarks = [{ id: 470, time: 23432 }, { id: 453, time: 234324 }, { id: 445, time: 987834 }], zipped = videos.map((v, i) => ({ [v.id]: bookmarks[i].id })); console.log(zipped); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Your first attempt doesn't work because the key of the object you used is not correct. 您的第一次尝试无效,因为您使用的对象的密钥不正确。 When you use object literals to define an object, keys have to be numeric or string literals. 当使用对象文字来定义对象时,键必须是数字或字符串文字。

On the other hand, you could solve that problem using array-like notation to create that key in the object. 另一方面,您可以使用类似数组的表示法在对象中创建键来解决该问题。 In JS there are two ways of accessing the value of a key within an object: using dot notation or array-like notation. 在JS中,有两种访问对象内键值的方法:使用点表示法或类似数组的表示法。 The array-like notation allows you to access keys of an object not determined until runtime (keys that are variables). 类似于数组的表示法允许您访问直到运行时才确定的对象的键(键为变量)。 For instance: 例如:

 var obj = {}; for(var i = 0; i < 3; i++) obj[i] = i + 1; console.log(obj); 

In case you're interested, there isn't much difference in terms of performance between the two. 如果您有兴趣,两者之间的性能没有太大区别。

Getting back to your problem. 回到您的问题。 Zip is a function that, in words of functional programmers, takes a pair of lists and creates a list of pairs. 用函数程序员的话来说,Zip是一个函数,它接受一对列表并创建一个对列表。 From my point of view (and a functional point of view I think), it might be wiser to tackle it differently from what you've done. 从我的角度(以及我认为的功能角度)来看,与您已完成的工作不同的方法可能更明智。

For instance (and just as a quick idea), you could create a function that does the zip (typical zip) and apply to the result to another function that picks the values you're interested in: 例如(出于一个快速的想法),您可以创建一个执行zip(典型zip)的函数,并将结果应用于另一个函数,该函数将选择您感兴趣的值:

 var videos = [{ "id": 70111470, "title": "Die Hard" }, { "id": 654356453, "title": "Bad Boys" }, { "id": 65432445, "title": "The Chamber" }], bookmarks = [{ id: 470, time: 23432 }, { id: 453, time: 234324 }, { id: 445, time: 987834 }]; // Assuming same size. function zip(list1, list2) { return list1.length ? [[list1[0], list2[0]]].concat(zip(list1.slice(1), list2.slice(1))) : []; } function makeObj(list) { var obj = {}; obj[list[0].id] = list[1].id; return obj; } console.log(zip(videos, bookmarks).map(makeObj)); 

I made a simple zip using recursion (there are optimal ways to do it, and also can be done taking into account that the lists can have different sizes) but I think it can help you to grasp the idea. 我使用递归制作了一个简单的zip(有最佳的方式来做,也可以考虑到列表可以有不同的大小),但是我认为它可以帮助您理解这个想法。

Hope it helps 希望能帮助到你

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

相关问题 如何在React中映射具有动态键的对象? - How to map the objects that having the dynamic keys in React? Javascript:使用未知键映射对象数组 - Javascript: map Array of Objects with unknown keys 如何将javascript字典键映射到新键 - how to map javascript dictionary keys to new keys 将对象作为键(不是字符串而是唯一对象)的关联映射的javascript方法是什么? - What is the javascript approach to an associative map with objects as keys (not strings but unique objects)? 如何在 Javascript 中的 Map 键上使用 .map() - How to use .map() over Map keys in Javascript 如何访问作为js Map中对象的键的值 - How to access values of keys that are objects in js Map 如何使用最新的 Javascript 合并两个对象,使一个对象的键映射到另一个对象的值,而第二个对象没有额外的键? - How do I merge two objects such that keys from one map to values of another, without the extra keys in the second, using latest Javascript? 如何通过javascript插入对象? - How to insert objects through javascript? 使用地图对动态JSON键进行分组,并尽可能减少javascript - Grouping dynamic JSON Keys using map and reduce if possible javascript 以对象为键的 Typescript Map - Typescript Map with objects as keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM