打字稿:如何将值设置为空字符串
Typescript: How to set value to empty string
我正在根据用 _
(下划线)分隔的第一个单词对数据进行排序和分组。所以结果应该是 return 3 组(例如:orders, items and ""
)。我如何为该空字符串设置一些值(例如:其他)。这里是stackblitz
let rawData = [
{ name : 'orders_list', id : 10},
{ name : 'orders_price', id : 7},
{ name : 'items_list', id : 12 },
{ name : 'items_price', id : 7},
{ name : 'others-list', id : 7},
{ name : 'secondOthers-list', id : 7},
]
let data = rawData.reduce((r, e) => {
let group = e.name.substr(0, e.name.indexOf('_'));
if(!r[group]) {
r[group] = {group, children: [e]}
} else {
r[group].children.push(e);
}
return r;
}, {})
let result = Object.values(data)
console.log(result)
这是我做的一个有效的 stackblitz :)
// Import stylesheets
import './style.css';
// Write TypeScript code!
const appDiv: HTMLElement = document.getElementById('app');
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`;
let rawData = [
{ name : 'orders_list', id : 10},
{ name : 'orders_price', id : 7},
{ name : 'items_list', id : 12 },
{ name : 'items_price', id : 7},
{ name : 'others-list', id : 7},
{ name : 'secondOthers-list', id : 7},
]
let data = rawData.reduce((r, e) => {
let group = e.name.substr(0, e.name.indexOf('_'));
if(!r[group]) {
r[group] = {group, children: [e]}
} else {
r[group].children.push(e);
}
return r;
}, {})
// since data at this point is an object, to get array of values
// we use Object.values method
let result = Object.values(data)
var resultMap = result.map(_val => {
return _val.group
})
var index = resultMap.indexOf('')
result[index].group = 'Others'
console.log(result)
检查这个。
// Import stylesheets
import './style.css';
// Write TypeScript code!
const appDiv: HTMLElement = document.getElementById('app');
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`;
let rawData = [
{ name : 'orders_list', id : 10},
{ name : 'orders_price', id : 7},
{ name : 'items_list', id : 12 },
{ name : 'items_price', id : 7},
{ name : 'others-list', id : 7},
{ name : 'secondOthers-list', id : 7},
]
let data = rawData.reduce((r, e) => {
let group = e.name.substr(0, e.name.indexOf('_'));
if(!r[group]) {
r[group] = {group, children: [e]}
} else {
r[group].children.push(e);
}
return r;
}, {})
// since data at this point is an object, to get array of values
// we use Object.values method
let result = Object.values(data)
result.filter(f=>{
if(f.group==''){
f.group='Others'
}
});
console.log(result)
我正在根据用 _
(下划线)分隔的第一个单词对数据进行排序和分组。所以结果应该是 return 3 组(例如:orders, items and ""
)。我如何为该空字符串设置一些值(例如:其他)。这里是stackblitz
let rawData = [
{ name : 'orders_list', id : 10},
{ name : 'orders_price', id : 7},
{ name : 'items_list', id : 12 },
{ name : 'items_price', id : 7},
{ name : 'others-list', id : 7},
{ name : 'secondOthers-list', id : 7},
]
let data = rawData.reduce((r, e) => {
let group = e.name.substr(0, e.name.indexOf('_'));
if(!r[group]) {
r[group] = {group, children: [e]}
} else {
r[group].children.push(e);
}
return r;
}, {})
let result = Object.values(data)
console.log(result)
这是我做的一个有效的 stackblitz :)
// Import stylesheets
import './style.css';
// Write TypeScript code!
const appDiv: HTMLElement = document.getElementById('app');
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`;
let rawData = [
{ name : 'orders_list', id : 10},
{ name : 'orders_price', id : 7},
{ name : 'items_list', id : 12 },
{ name : 'items_price', id : 7},
{ name : 'others-list', id : 7},
{ name : 'secondOthers-list', id : 7},
]
let data = rawData.reduce((r, e) => {
let group = e.name.substr(0, e.name.indexOf('_'));
if(!r[group]) {
r[group] = {group, children: [e]}
} else {
r[group].children.push(e);
}
return r;
}, {})
// since data at this point is an object, to get array of values
// we use Object.values method
let result = Object.values(data)
var resultMap = result.map(_val => {
return _val.group
})
var index = resultMap.indexOf('')
result[index].group = 'Others'
console.log(result)
检查这个。
// Import stylesheets
import './style.css';
// Write TypeScript code!
const appDiv: HTMLElement = document.getElementById('app');
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`;
let rawData = [
{ name : 'orders_list', id : 10},
{ name : 'orders_price', id : 7},
{ name : 'items_list', id : 12 },
{ name : 'items_price', id : 7},
{ name : 'others-list', id : 7},
{ name : 'secondOthers-list', id : 7},
]
let data = rawData.reduce((r, e) => {
let group = e.name.substr(0, e.name.indexOf('_'));
if(!r[group]) {
r[group] = {group, children: [e]}
} else {
r[group].children.push(e);
}
return r;
}, {})
// since data at this point is an object, to get array of values
// we use Object.values method
let result = Object.values(data)
result.filter(f=>{
if(f.group==''){
f.group='Others'
}
});
console.log(result)