简体   繁体   English

JavaScript:将二维数组与 Map 转换为字典(嵌套对象)

[英]JavaScript: Convert two-dimensional array with Map into Dictionary(nested Object)

I have a set of values in a two-dimensional array.我在二维数组中有一组值。 I mapped an Object and made a new array.我映射了一个 Object 并制作了一个新数组。 The first value key is a string and the second value currentTasks[key] is an array where each value has id , text , and date .第一个值key是一个字符串,第二个值currentTasks[key]是一个数组,其中每个值都有idtextdate

const newArray = Object.keys(currentTasks).map(function(key){
                return [key, currentTasks[key]];
            });

The newArray has a form like this… newArray 有这样的形式……

['1', {id:'1', text: "Todo item #1", date: '2021-11-30'}]

What I want to do is to convert the newArray into the form of a Dictionary.我想要做的是将 newArray 转换为 Dictionary 的形式。 I want to make the first value as a key, and the second value, in a HashMap form, as a value.我想以 HashMap 形式将第一个值作为键,将第二个值作为值。 How can I convert this form of the array into a hash map like a key-value pair?如何将这种形式的数组转换为像键值对一样的 hash map?

{
        '1': {id:'1', text: "Todo item #1", date: '2021-11-30'},
        '2': {id:'2', text: "Todo item #2", date: '2021-12-12'},
        '3': {id:'3', text: "Todo item #3", date: '2021-11-29'},
        '4': {id:'4', text: "Todo item #4", date: '2021-12-03'},
}

I want to get a nested object array like this.我想得到一个像这样的嵌套 object 数组。

 const testValues = [ ['1', {id:'1', text: "Todo item #1", date: '2021-11-30'}], ['2', {id:'2', text: "Todo item #2", date: '2021-12-12'}], ['3', {id:'3', text: "Todo item #3", date: '2021-11-29'}], ['4', {id:'4', text: "Todo item #4", date: '2021-12-03'}] ]; const outputObject = {}; testValues.forEach(x => { outputObject[x[0]] = x[1]; }); console.log(testValues, outputObject);

If you are into brevity, I believe you are looking for the reduce array method.如果您想简洁,我相信您正在寻找reduce数组方法。

The reduce() method executes a user-supplied “reducer” callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. reduce() 方法按顺序对数组的每个元素执行用户提供的“reducer”回调 function,并传入前一个元素的计算返回值。 The final result of running the reducer across all elements of the array is a single value.在数组的所有元素上运行 reducer 的最终结果是单个值。

You can provide an initial value, which in this case, is an object literal.您可以提供一个初始值,在这种情况下,它是一个 object 文字。 You can then map over each sub array and provide the key: value pairs.然后,您可以在每个子阵列上 map 并提供键:值对。

 const array1 = [ ['1', {id:'1', text: "Todo item #1", date: '2021-11-30'}], ['2', {id:'2', text: "Todo item #2", date: '2021-12-12'}], ['3', {id:'3', text: "Todo item #3", date: '2021-11-29'}], ['4', {id:'4', text: "Todo item #4", date: '2021-12-03'}] ]; const reducer = (previousValue, currentValue) => ({...previousValue, [currentValue[0]]: currentValue[1]}); // {1: {…}, 2: {…}, 3: {…}, 4: {…}} console.log(array1.reduce(reducer, {})); // as a one liner // array1.reduce((a, b) => ({...a, [b[0]]: b[1]}), {})

Or You could also use a Map或者您也可以使用Map

The Map object holds key-value pairs and remembers the original insertion order of the keys. Map object 保存键值对并记住键的原始插入顺序。 Any value (both objects and primitive values) may be used as either a key or a value.任何值(对象和原始值)都可以用作键或值。

 const array1 = [ ['1', {id:'1', text: "Todo item #1", date: '2021-11-30'}], ['2', {id:'2', text: "Todo item #2", date: '2021-12-12'}], ['3', {id:'3', text: "Todo item #3", date: '2021-11-29'}], ['4', {id:'4', text: "Todo item #4", date: '2021-12-03'}] ]; const newMap = new Map() array1.forEach(el => newMap.set(el[1], el[0])) // 1: {…}, 2: {…}, 3: {…}, 4: {…} newMap.forEach((key, value) => console.log(key, value))

I guess this is the output you are looking for?:我猜这就是您要找的 output?:

 const values = [ ['1', {id:'1', text: "Todo item #1", date: '2021-11-30'}], ['2', {id:'2', text: "Todo item #2", date: '2021-12-12'}], ['3', {id:'3', text: "Todo item #3", date: '2021-11-29'}], ['4', {id:'4', text: "Todo item #4", date: '2021-12-03'}] ] const obj = {}; values.map(val => obj[val[0]] = val[1]) console.log(obj)

Use Object.fromEntries -使用Object.fromEntries -

 const input = [ {id:'1', text: "Todo item #1", date: '2021-11-30'}, {id:'2', text: "Todo item #2", date: '2021-12-12'}, {id:'3', text: "Todo item #3", date: '2021-11-29'}, {id:'4', text: "Todo item #4", date: '2021-12-03'}, ] const output = Object.fromEntries(input.map(x => [x.id, x])) console.log(output)

{
  '1': {id:'1', text: "Todo item #1", date: '2021-11-30'},
  '2': {id:'2', text: "Todo item #2", date: '2021-12-12'},
  '3': {id:'3', text: "Todo item #3", date: '2021-11-29'},
  '4': {id:'4', text: "Todo item #4", date: '2021-12-03'},
}

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

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