[英]Repeat every element in array based on object properties
I have an array that I'm retrieving from an API.我有一个从 API 中检索的数组。 The array looks like this:
数组如下所示:
[{
"name": "Rachel",
"count": 4,
"fon": "46-104104",
"id": 2
},
{
"name": "Lindsay",
"count": 2,
"fon": "43-053201",
"id": 3
},
{
"name": "Michael",
"count": 5,
"fon": "46-231223",
"id": 4
}]
Then I loop through the array to create an array containing only the names.然后我循环遍历数组以创建一个仅包含名称的数组。
function buildName(data) {
for (var i = 0; i < data.length; i++) {
nameList.push(data[i].name)
}
}
This also works so far, but I would like to create an array in which each name occurs as often as the object count says.到目前为止,这也有效,但我想创建一个数组,其中每个名称的出现频率与 object 计数所说的一样。
For example, the name Michael should appear five times in the array and Lindsay twice.例如,名字 Michael 应该在数组中出现五次,而 Lindsay 应该出现两次。
[
"Rachel",
"Rachel",
"Rachel",
"Rachel",
"Lindsay",
"Lindsay",
"Michael",
"Michael",
"Michael",
"Michael"
"Michael"
]
For each object create a new array using count
, and then fill
it with the name
.为每个 object 使用
count
创建一个新数组,然后用name
fill
它。
If you use flatMap
to iterate over the array of objects.如果您使用
flatMap
迭代对象数组。 It will return a new array of nested objects but then flatten them into a non-nested structure.它将返回一个新的嵌套对象数组,然后将它们展平为非嵌套结构。
const data=[{name:"Rachel",count:4,fon:"46-104104",id:2},{name:"Lindsay",count:2,fon:"43-053201",id:3},{name:"Michael",count:5,fon:"46-231223",id:4}]; const out = data.flatMap(obj => { return new Array(obj.count).fill(obj.name) }); console.log(out);
Use an inner while
loop inside the for
loop:在
for
循环中使用内部while
循环:
const data = [{ "name": "Rachel", "count": 4, "fon": "46-104104", "id": 2 }, { "name": "Lindsay", "count": 2, "fon": "43-053201", "id": 3 }, { "name": "Michael", "count": 5, "fon": "46-231223", "id": 4 }] function buildName(data){ const result = []; for (let i = 0; i < data.length; i += 1) { let item = data[i]; let count = item.count; while (count > 0) { result.push(item.name); count -= 1; } } return result; } console.log(buildName(data));
You can use .flapMap()
for that:您可以为此使用
.flapMap()
:
const arr = [{ "name": "Rachel", "count": 4, "fon": "46-104104", "id": 2 }, { "name": "Lindsay", "count": 2, "fon": "43-053201", "id": 3 }, { "name": "Michael", "count": 5, "fon": "46-231223", "id": 4 }]; const result = arr.flatMap(({count, name}) => Array(count).fill(name)); console.log(result);
Effectively you turn every element into an array of the the name property repeated count
times which is then flattened into a single array.实际上,您将每个元素转换为 name 属性重复
count
次数的数组,然后将其展平为单个数组。
Just add an inner loop with as many iterations as the "count"
property in the object:只需在 object 中添加一个迭代次数与
"count"
属性一样多的内部循环:
function buildName(data) {
const nameList = [];
for (var i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].count; j++) {
nameList.push(data[i].name);
}
}
return nameList;
}
I've upgraded your functions but you can use the map method我已经升级了你的功能,但你可以使用 map 方法
function buildName(data){
for (let i = 0; i < data.length; i++){
let numToLoop = data[i].count
let name = data[i].name
for (let z = 0; z < +numToLoop; z++){
nameList.push(name)
}
}
}
For fun为了娱乐
import { pipe } from 'fp-ts/lib/function';
import { chain, replicate } from 'fp-ts/lib/Array';
const arr = ...
const result = pipe(
arr,
chain(i => replicate(i.count, i.name))
);
It can be done via creating an array with repeated names in this way:可以通过以这种方式创建具有重复名称的数组来完成:
Array(count).fill(name)
Then you have to spread it into resulting array.然后你必须将它传播到结果数组中。 You can try this one-liner
你可以试试这个单线
const getNames = (data) =>
data.reduce(
(names, { name, count }) => [...names, ...Array(count).fill(name)],
[]
)
Note that a pure function is presented here, which is generally the preferred way of writing code.注意这里呈现的是一个纯function,这通常是编写代码的首选方式。 However, updating your example code might look like this
但是,更新您的示例代码可能如下所示
const getNames = (data) =>
data.reduce(
(names, { name, count }) => [...names, ...Array(count).fill(name)],
[]
)
function buildName(data) {
nameList = getNames(data)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.