Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function transformData(data, types) {
- const processedColors = {};
- data.forEach((item, index) => {
- // Extracts color property from the item
- const { color } = item;
- if (processedColors[color]) {
- const previousTypeIndex = types.indexOf(processedColors[color]);
- let nextItemIndex = previousTypeIndex + 1;
- if (types.length > nextItemIndex) {
- item.lineStyle = types[nextItemIndex] || '';
- } else {
- item.lineStyle = '';
- }
- processedColors[color] = item.lineStyle;
- } else {
- item.lineStyle = types[0];
- processedColors[color] = types[0];
- }
- });
- return data;
- }
- let data = [
- {
- color: 'green'
- },
- {
- color: 'green'
- },
- {
- color: 'green'
- },
- {
- color: 'green'
- }
- ];
- let types = ['solid', 'dashed', 'dotted'];
- data = transformData(data, types);
- console.log(data);
Advertisement
Add Comment
Please, Sign In to add comment