kar*_*tik 24 javascript arrays ecmascript-6
optimizedRoute = ['Bengaluru', 'Salem', 'Erode', 'Tiruppur', 'Coimbatore']
result = [
{start: bengaluru, end: salem},
{start: salem, end: erode},
{start: erode, end: tiruppur},
{start: tiruppur, end: coimbatore},
]
Run Code Online (Sandbox Code Playgroud)
我想转换optimizedRoute成结果.我想用ES6做到这一点.reduce().这是我尝试过的:
const r = optimizedRoute.reduce((places, place, i) => {
const result: any = [];
places = []
places.push({
startPlace: place,
endPlace: place
});
// result.push ({ startplace, endplace, seats: 4 });
// console.log(result);
return places;
}, {});
console.log(r)
Run Code Online (Sandbox Code Playgroud)
Nin*_*olz 14
您可以使用reduce获取路径的开始和结束部分并返回结束以进行下一次启动.
getParts = a => ( // take a as array and return an IIFE
r => ( // with an initialized result array
a.reduce((start, end) => ( // reduce array by taking two values
r.push({ start, end }), // push short hand properties
end // and take the last value as start value for next loop
)),
r // finally return result
)
)([]); // call IIFE with empty array
Run Code Online (Sandbox Code Playgroud)
const getParts = a => (r => (a.reduce((start, end) => (r.push({ start, end }), end)), r))([]);
var optimizedRoute = ['Bengaluru', 'Salem', 'Erode', 'Tiruppur', 'Coimbatore']
console.log(getParts(optimizedRoute));Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }Run Code Online (Sandbox Code Playgroud)
@EDIT了GrégoryNEUT 加入交代
// Two thing to know first :
// When no initial value is provided,
// Array.reduce takes the index 0 as first value and start to loop at index 1
// Doing (x, y, z)
// Will execute the code x, y and z
// Equivalent to :
// x;
// y;
// z;
let ex = 0;
console.log((ex = 2, ex = 5, ex = 3));
// So about the code
const getParts = (a) => {
// We are creating a new function here so we can have an array where to
// push data to
const func = (r) => {
// Because there is no initial value
//
// Start will be the value at index 0 of the array
// The loop is gonna start at index 1 of the array
a.reduce((start, end) => {
console.log(start, end);
r.push({
start,
end,
});
return end;
});
return r;
};
return func([]);
};
// Equivalent
const getPartsEquivalent = (a) => {
const r = [];
// Because there is no initial value
//
// Start will be the value at index 0 of the array
// The loop is gonna start at index 1 of the array
a.reduce((start, end) => {
console.log(start, end);
r.push({
start,
end,
});
return end;
});
return r;
};
var optimizedRoute = ['Bengaluru', 'Salem', 'Erode', 'Tiruppur', 'Coimbatore']
console.log(getPartsEquivalent(optimizedRoute));Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}Run Code Online (Sandbox Code Playgroud)
Mih*_*nut 11
另一种方法是map 结合使用方法slice.对于map函数,您必须传递一个callback函数作为参数,该函数将应用于给定数组中的每个项目.
optimizedRoute = ['Bengaluru', 'Salem', 'Erode', 'Tiruppur', 'Coimbatore']
var result = optimizedRoute
.slice(0, -1)
.map((item, index) => ({start : item, end : optimizedRoute[index + 1]}));
console.log(result);Run Code Online (Sandbox Code Playgroud)
Tom*_*ech 11
我并不真正理解"with reduce"的要求,因为使用循环的相应代码可以立即读取,不需要解释:
const optimizedRoute = ['Bengaluru', 'Salem', 'Erode', 'Tiruppur', 'Coimbatore'];
const result = new Array(optimizedRoute.length - 1);
for (let i = 0; i < result.length; ++i) {
result[i] = {
start: optimizedRoute[i],
end: optimizedRoute[i + 1]
};
}
console.log(result)Run Code Online (Sandbox Code Playgroud)
有时候做一些聪明的事情很有趣,但是与此相比,有些答案非常复杂!
这是一个带有reduce. 不过,我不确定这是最自然的方法!
使用reduce感觉有点矫枉过正,在那种情况下(但这只是我的意见),我自然会使用索引,好吧,我会选择一个简单的for循环。
const optimizedRoute = ['Bengaluru', 'Salem', 'Erode', 'Tiruppur', 'Coimbatore'];
let startCity;
const result = optimizedRoute.reduce((acc, city) => {
if(startCity) {
acc.push({start: startCity, end: city});
}
startCity = city;
return acc;
}, []);
console.log(result);Run Code Online (Sandbox Code Playgroud)