how to create a new array from two arrays, and take from one the length and from other the values?

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

how to create a new array from two arrays, and take from one the length and from other the values?

问题

我有两个数组

    list_a = [20, 20, 20, 21, 21, 22, 20,21, 23, 20, 23, 24]
    list_b = [120, 121, 122, 123, 124]

我想从这两个数组创建下一个数组如下所示

    newList = [120, 120, 120, 121, 121, 122, 120, 121, 123, 120, 123, 124]

因此第一个数组中的第一个数字将被值120替换但不是在长度为20时依此类推之后当也定位到20时它将用第一个数字替换

我尝试了很多方法但都不起作用...
英文:

I have two arrays:

list_a = [20, 20, 20, 21, 21, 22, 20,21, 23, 20, 23, 24]
list_b = [120, 121, 122, 123, 124]

and I want to create the next array from those two arrays like so:

newList = [120, 120, 120, 121, 121, 122, 120, 121, 123, 120, 123, 124]

so the first number from the first array will replaced by value with 120, but not in the length of the 20, and so on, and afterwards when 20 is also located, it will replace it with the first number also

I tried a lot of stuff, nothing worked...

答案1

得分: 1

你可以采用一个对象来存储数值,并保持未见数值的索引。

const
    list_a = [20, 20, 20, 21, 21, 22, 20, 21, 23, 20, 23, 24],
    list_b = [120, 121, 122, 123, 124],
    result = list_a.map(
        ((values, i) => v => values[v] ?? (values[v] = list_b[i++]))
        ({}, 0)
    );

console.log(...result);
console.log(...[120, 120, 120, 121, 121, 122, 120, 121, 123, 120, 123, 124]);
英文:

You could take an object for the values and keep an index of unseen values.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
list_a = [20, 20, 20, 21, 21, 22, 20, 21, 23, 20, 23, 24],
list_b = [120, 121, 122, 123, 124],
result = list_a.map(
((values, i) => v => values[v] ?? (values[v] = list_b[i++]))
({}, 0)
);

console.log(...result);
console.log(...[120, 120, 120, 121, 121, 122, 120, 121, 123, 120, 123, 124]);

<!-- end snippet -->

答案2

得分: 0

The specs were not clear

Try this, we create a unique list of list_a and use the placement of each to index into list_b

const list_a = [20, 20, 20, 21, 21, 22, 20, 21, 23, 20, 23, 24]
const list_b = [120, 121, 122, 123, 124]
const idxList = [...new Set(list_a)]; // unique and placements
const newList = list_a.map(num => {
  const idx = idxList.indexOf(num)
  return list_b[idx]
});

console.log(newList)
英文:

The specs were not clear

Try this, we create a unique list of list_a and use the placement of each to index into list_b

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

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

const list_a = [20, 20, 20, 21, 21, 22, 20, 21, 23, 20, 23, 24]
const list_b = [120, 121, 122, 123, 124]
const idxList = [...new Set(list_a)]; // unique and placements
const newList = list_a.map(num =&gt; {
  const idx = idxList.indexOf(num)
  return list_b[idx]
});

console.log(newList)

<!-- end snippet -->

答案3

得分: 0

这个问题可能有一个更加优雅的解决方案,可以使用Sets,但我首先想到的是先创建一个hash map,然后通过map方法遍历list_a数组。

这个解决方案允许list_b中有任何值,需要满足以下条件:

unique_values_list_b.length >= unique_values_list_a

希望这能帮助你或者指导你朝着正确的方向前进。

(function main() {
  const list_a = [20, 20, 20, 21, 21, 22, 20, 21, 23, 20, 23, 24];
  const list_b = [120, 121, 122, 123, 124];

  let i = 0;
  const hash = new Map([]);

  // 设置一个哈希映射
  for (let a of list_a) {
    if (hash.has(a)) continue;

    hash.set(a, list_b[i++]);
  }

  // 创建你的新列表

  const newList = list_a.map(element => hash.get(element));
  console.log(newList);
})();

请注意,这只是代码的翻译,没有其他内容。

英文:

There's probably a more elegant solution with Sets but the first thing that came to my mind was to make a hash map first and then map through the list_a array.

This solution allows for any values in list_b but requires that the following condition is met:

unique_values_list_b.length &gt;= unique_values_list_a

Hope this helps or sends you in the right direction.

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

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

(function main () {
	const list_a = [20, 20, 20, 21, 21, 22, 20,21, 23, 20, 23, 24]
	const list_b = [120, 121, 122, 123, 124]

	let i = 0
	const hash = new Map([])

	// setting up a hash
	for(let a of list_a){
		if(hash.has(a)) continue
	
		hash.set(a, list_b[i++])		
	}


	// creating your new list

	const newList = list_a.map(element =&gt; hash.get(element))
	console.log(newList)
})()

<!-- end snippet -->

答案4

得分: 0

let list_a = [20, 20, 20, 21, 21, 22, 20, 21, 23, 20, 23, 24];
let list_b = [120, 121, 122, 123, 124];

let output = list_a.map((e) => {
  return parseInt('1' + e);
});

console.log(output);
英文:

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

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

let list_a = [20, 20, 20, 21, 21, 22, 20, 21, 23, 20, 23, 24];
let list_b = [120, 121, 122, 123, 124];

let output = list_a.map((e) =&gt; {
  return parseInt(&#39;1&#39; + e);
});

console.log(output);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月25日 23:47:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551221.html
匿名

发表评论

匿名网友

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

确定