将列表类型数据解析为不同格式以兼容电子表格
Parse list type data into different format to make compatible for spreadsheet
我是JavaScript的新人,反应过来 如果不能正确解释问题,请post评论,这样至少我可以告诉更多。
如果有人能给我一点帮助,我将不胜感激
我已经被这个问题难住了好几个小时,我想不出解决这个问题的办法。
假设我有列表类型的数据,需要从一种格式解析为另一种格式,这样我就可以与 react-datasheet 格式兼容以显示电子表格
Sample data:
const sampleData = [
{
question: "what is your name?",
answer: "Ben",
topic: "names"
},
{
question: "what is your name?",
answer: "Will",
topic: "names"
},
{
question: "What is your brother's age?",
answer: 55,
topic: "ages"
}
]
Expected results
grid: [
[
{ readOnly: true, value: "SL" },
{ value: "topic", readOnly: true },
{ value: "question", readOnly: true },
{ value: "answer", readOnly: true },
],
[
{ readOnly: true, value: 1 },
{ value: 'names' },
{ value: ' what is your name?' },
{ value: 'Ben' },
],
[
{ readOnly: true, value: 2 },
{ value:'names' },
{ value: 'what is your name?' },
{ value: 'Willi' },
],
[
{ readOnly: true, value: 3 },
{ value: 'ages' },
{ value: "What is your brother's age?" },
{ value: 33 },
],
]
const sampleData = [
{
question: "what is your name?",
answer: "Ben",
topic: "names"
},
{
question: "what is your name?",
answer: "Will",
topic: "names"
},
{
question: "What is your brother's age?",
answer: 55,
topic: "ages"
}
]
// First, for each object in sampleData, construct an array of objects:
var grid = sampleData.map(function(currentValue, index, a) {
return [
{ readOnly: true, value: index + 1 },
{ value: currentValue.topic },
{ value: currentValue.question },
{ value: currentValue.answer }
];
});
// Then prepend the "header/config" array of objects:
grid.unshift([
{ readOnly: true, value: "SL" },
{ value: "topic", readOnly: true },
{ value: "question", readOnly: true },
{ value: "answer", readOnly: true },
]);
// Finally, wrap grid in an object:
var expectedResults = {
grid: grid
}
console.log(expectedResults);
我是JavaScript的新人,反应过来 如果不能正确解释问题,请post评论,这样至少我可以告诉更多。
如果有人能给我一点帮助,我将不胜感激
我已经被这个问题难住了好几个小时,我想不出解决这个问题的办法。
假设我有列表类型的数据,需要从一种格式解析为另一种格式,这样我就可以与 react-datasheet 格式兼容以显示电子表格
Sample data:
const sampleData = [
{
question: "what is your name?",
answer: "Ben",
topic: "names"
},
{
question: "what is your name?",
answer: "Will",
topic: "names"
},
{
question: "What is your brother's age?",
answer: 55,
topic: "ages"
}
]
Expected results
grid: [
[
{ readOnly: true, value: "SL" },
{ value: "topic", readOnly: true },
{ value: "question", readOnly: true },
{ value: "answer", readOnly: true },
],
[
{ readOnly: true, value: 1 },
{ value: 'names' },
{ value: ' what is your name?' },
{ value: 'Ben' },
],
[
{ readOnly: true, value: 2 },
{ value:'names' },
{ value: 'what is your name?' },
{ value: 'Willi' },
],
[
{ readOnly: true, value: 3 },
{ value: 'ages' },
{ value: "What is your brother's age?" },
{ value: 33 },
],
]
const sampleData = [
{
question: "what is your name?",
answer: "Ben",
topic: "names"
},
{
question: "what is your name?",
answer: "Will",
topic: "names"
},
{
question: "What is your brother's age?",
answer: 55,
topic: "ages"
}
]
// First, for each object in sampleData, construct an array of objects:
var grid = sampleData.map(function(currentValue, index, a) {
return [
{ readOnly: true, value: index + 1 },
{ value: currentValue.topic },
{ value: currentValue.question },
{ value: currentValue.answer }
];
});
// Then prepend the "header/config" array of objects:
grid.unshift([
{ readOnly: true, value: "SL" },
{ value: "topic", readOnly: true },
{ value: "question", readOnly: true },
{ value: "answer", readOnly: true },
]);
// Finally, wrap grid in an object:
var expectedResults = {
grid: grid
}
console.log(expectedResults);