使用两个不同的值作为范围填充Javascript中的对象数组
具有具有以下结构的对象数组:使用两个不同的值作为范围填充Javascript中的对象数组,javascript,arrays,json,data-wrangling,Javascript,Arrays,Json,Data Wrangling,具有具有以下结构的对象数组: [{value: "a", start: 1950, end: 1954, description: "aaa"}, {value: "b", start: 1953, end: 1956, description: "bbb"}, {value: "c", start: 1960, end: 1962, description: "ccc"}]
[{value: "a", start: 1950, end: 1954, description: "aaa"},
{value: "b", start: 1953, end: 1956, description: "bbb"},
{value: "c", start: 1960, end: 1962, description: "ccc"}]
如何使用javascript填充数组以获得以下结果?
我想填充数组,在开始
和结束
之间,每年创建一个新对象
[{value: "a", year: 1950, start: 1950, end: 1954, description: "aaa"},
{value: "a", year: 1951, start: 1950, end: 1954, description: "aaa"},
{value: "a", year: 1952, start: 1950, end: 1954, description: "aaa"},
{value: "a", year: 1953, start: 1950, end: 1954, description: "aaa"},
{value: "a", year: 1954, start: 1950, end: 1954, description: "aaa"},
{value: "b", year: 1953 ,start: 1953, end: 1956, description: "bbb"},
{value: "b", year: 1954, start: 1953, end: 1956, description: "bbb"},
{value: "b", year: 1955, start: 1953, end: 1956, description: "bbb"},
{value: "b", year: 1956, start: 1953, end: 1956, description: "bbb"},
{value: "c", year: 1960, start: 1960, end: 1962, description: "ccc"},
{value: "c", year: 1961, start: 1960, end: 1962, description: "ccc"},
{value: "c", year: 1962, start: 1960, end: 1962, description: "ccc"}]
我的理解是,我应该编写一个for循环来映射结果,但我很难理解如何解释每个原始值的开始
和结束
变量
const数据=[
{值:“a”,开始时间:1950年,结束时间:1954年,描述:“aaa”},
{值:“b”,开始时间:1953年,结束时间:1956年,说明:“bbb”},
{值:“c”,开始时间:1960年,结束时间:1962年,描述:“ccc”}
];
const result=data.flatMap({value,start,end,description})=>{
返回数组。from({length:end-start+1},({u,i)=>start+i)
.map(年=>({值,年,开始,结束,说明}));
});
控制台日志(结果)代码>请,拿一个,读一读,然后创建一个{value:“a”,年份:1953年,开始:1950年,结束:1954年,描述:“aaa”},
你能解释一下这句话吗?同样,请展示你的尝试