Javascript 如何在react native中使用for循环遍历数组?
我正在尝试在react native中循环json数据。我想创建一个具有不同Javascript 如何在react native中使用for循环遍历数组?,javascript,json,react-native,for-loop,Javascript,Json,React Native,For Loop,我正在尝试在react native中循环json数据。我想创建一个具有不同键的新数组,并且值将是循环的json结果。我尝试了以下操作,但没有按预期工作。json响应的格式如下所示 json 0: {key: 0, id: 0, type: "section", title: "A1. India's Economic Development", duration: 0, …} 1: {key: 1, id: "1", type: "unit", title: "1. India as
键的新数组,并且值将是循环的json结果。我尝试了以下操作,但没有按预期工作。json响应的格式如下所示
json
0: {key: 0, id: 0, type: "section", title: "A1. India's Economic Development", duration: 0, …}
1: {key: 1, id: "1", type: "unit", title: "1. India as a Developing Economy", duration: 0, …}
2: {key: 2, id: "2", type: "unit", title: "2. Understanding India’s economic transition", duration: 0, …}
3: {key: 17, id: 0, type: "section", title: "A2. National Income", duration: 0, …}
4: {key: 18, id: "5", type: "unit", title: "1. India in the global economy", duration: 0, …}
5: {key: 19, id: "6", type: "unit", title: "2. China, India and the rise of Asia", duration: 0, …}
我想要一个这样的数组
const dataArray = [
{
title: "India's Economic Development",
content:
"India as a Developing Economy",
"Understanding India’s economic transition"
},
{
title: "National Income",
content:
"India in the global economy",
"China, India and the rise of Asia"
}
]
下面是我做过的循环,但什么也没有发生。请帮忙
.then((response) => response.json())
.then((responseData) => {
responseData.map(detail => {
let resultk = [];
//console.log( detail.data.curriculum);
for (var i = 0, j = 0; i < detail.data.curriculum.length; i++) {
curr = detail.data.curriculum;
console.log(curr.title);
if (curr.type === "section") {
resultk['title'] = curr.title;
this.result[j++] = resultk;
} else if (curr.type === "unit") {
resultk['content'] = curr.title;
}
}
console.log(resultk)
})
})
.then((response)=>response.json()
.然后((响应数据)=>{
responseData.map(详细信息=>{
设resultk=[];
//日志(细节、数据、课程);
对于(变量i=0,j=0;i
以下是您想要的完整示例代码,尝试使用最终循环更改数据,您将获得所需的输出:
testingggg = () => {
var data = {
0: {key: 0, id: 0, type: "section", title: "A1. India's Economic Development", duration: 0},
1: {key: 1, id: "1", type: "unit", title: "1. India as a Developing Economy", duration: 0},
2: {key: 2, id: "2", type: "unit", title: "2. Understanding India’s economic transition", duration: 0},
3: {key: 17, id: 0, type: "section", title: "A2. National Income", duration: 0},
4: {key: 18, id: "5", type: "unit", title: "1. India in the global economy", duration: 0},
5: {key: 19, id: "6", type: "unit", title: "2. China, India and the rise of Asia", duration: 0}
}
var keys = [];
for(var k in data) keys.push(k);
//alert("total " + keys.length + " keys: " + keys);
var dataArray = []
for(i=0;i<keys.length;i++)
{
var newObj = { // Change your required detail here
type: data[i].type,
title: data[i].title
}
dataArray.push(newObj);
}
console.log(dataArray);
}
testingggg=()=>{
风险值数据={
0:{key:0,id:0,键入:“section”,标题:“A1.印度的经济发展”,持续时间:0},
1:{关键字:1,id:“1”,类型:“单位”,标题:“1.印度作为一个发展中经济体”,持续时间:0},
2:{键:2,id:“2”,类型:“单位”,标题:“2.了解印度的经济转型”,持续时间:0},
3:{键:17,id:0,类型:“部分”,标题:“A2.国民收入”,持续时间:0},
4:{关键字:18,id:“5”,类型:“单位”,标题:“1.印度在全球经济中的地位”,持续时间:0},
5:{键:19,id:“6”,类型:“单位”,标题:“2.中国、印度和亚洲的崛起”,持续时间:0}
}
var键=[];
对于(数据中的var k)键,按(k);
//警报(“总计”+键。长度+”键:“+键);
var dataArray=[]
对于(i=0;i这里是一个完整的示例代码,说明您想要什么,请尝试使用最终循环更改数据,您将获得所需的输出:
testingggg = () => {
var data = {
0: {key: 0, id: 0, type: "section", title: "A1. India's Economic Development", duration: 0},
1: {key: 1, id: "1", type: "unit", title: "1. India as a Developing Economy", duration: 0},
2: {key: 2, id: "2", type: "unit", title: "2. Understanding India’s economic transition", duration: 0},
3: {key: 17, id: 0, type: "section", title: "A2. National Income", duration: 0},
4: {key: 18, id: "5", type: "unit", title: "1. India in the global economy", duration: 0},
5: {key: 19, id: "6", type: "unit", title: "2. China, India and the rise of Asia", duration: 0}
}
var keys = [];
for(var k in data) keys.push(k);
//alert("total " + keys.length + " keys: " + keys);
var dataArray = []
for(i=0;i<keys.length;i++)
{
var newObj = { // Change your required detail here
type: data[i].type,
title: data[i].title
}
dataArray.push(newObj);
}
console.log(dataArray);
}
testingggg=()=>{
风险值数据={
0:{key:0,id:0,键入:“section”,标题:“A1.印度的经济发展”,持续时间:0},
1:{关键字:1,id:“1”,类型:“单位”,标题:“1.印度作为一个发展中经济体”,持续时间:0},
2:{键:2,id:“2”,类型:“单位”,标题:“2.了解印度的经济转型”,持续时间:0},
3:{键:17,id:0,类型:“部分”,标题:“A2.国民收入”,持续时间:0},
4:{关键字:18,id:“5”,类型:“单位”,标题:“1.印度在全球经济中的地位”,持续时间:0},
5:{键:19,id:“6”,类型:“单位”,标题:“2.中国、印度和亚洲的崛起”,持续时间:0}
}
var键=[];
对于(数据中的var k)键,按(k);
//警报(“总计”+键。长度+”键:“+键);
var dataArray=[]
对于(i=0;i
如果resp是一个具有长度和0、1、2、…键的对象,请使用Array.from(obj)将其转换为对象
如果resp已排序(每个单元属于上一节)
要从标题中删除第一个单词,请使用
function removeFirstWord(str) {
return str.replace(/^[^\s]+\s/, '')
}
符号被称为正则表达式
- 该字符串以(第一个^sign)除字符外的任何字符开头
- 空白(空白=\s,[^something]表示不是某物)
- 加号表示最后一部分可以重复1次或更多次
到目前为止,它找到了第一个单词
- \s表示也替换单词后的空格
如果resp是一个具有长度和0、1、2、…键的对象,请使用Array.from(obj)将其转换为对象
如果resp已排序(每个单元属于上一节)
要从标题中删除第一个单词,请使用
function removeFirstWord(str) {
return str.replace(/^[^\s]+\s/, '')
}
符号被称为正则表达式
- 该字符串以(第一个^sign)除字符外的任何字符开头
- 空白(空白=\s,[^something]表示不是某物)
- 加号表示最后一部分可以重复1次或更多次
到目前为止,它找到了第一个单词
- \s表示也替换单词后的空格
使用reduce
函数和变量跟踪累加器阵列的索引
检查类型是否为节,然后在累加器数组中按下该值并将变量值更新1
如果类型为unit,则在内容中添加由currendex
变量定义的索引处的值
let值=[{
关键字:0,
id:0,
键入:“节”,
标题:“A1.印度的经济发展”,
持续时间:0
},
{
重点:1,,
id:“1”,
类型:“单位”,
标题:“1.印度作为发展中经济体”,
持续时间:0
},
{
重点:二,,
id:“2”,
类型:“单位”,
标题:“2.了解印度的经济转型”,
持续时间:0
},
{
关键字:17,
id:0,
键入:“节”,
标题:“A2.国民收入”,
持续时间:0
},
{
关键词:18,
id:“5”,
类型:“单位”,
标题:“1.全球经济中的印度”,
持续时间:0
},
{
关键字:19,
id:“6”,
类型:“单位”,
标题:“2.中国、印度和亚洲的崛起”,
持续时间:0
}
]
设currendex=-1;
设k=值。减少((acc,curr)=>{
if(curr.type==='section'){
加速推({
标题:curr.title.split('.')[1].trim(),
内容:[]
})
当前索引+=1
}否则{
acc[currendex].content.push(curr.title)
}
返回acc;
}, []);
console.log(k)
使用reduce
函数和变量跟踪累加器数组的索引
检查类型是否为节,然后在累加器数组中按下该值并将变量值更新1
如果类型为unit,则在内容中添加由currendex
变量定义的索引处的值
let值=[{
ke
var data = {
0: { key: 0, id: 0, type: "section", title: "A1. India's Economic Development", duration: 0 },
1: { key: 1, id: "1", type: "unit", title: "1. India as a Developing Economy", duration: 0 },
2: { key: 2, id: "2", type: "unit", title: "2. Understanding India’s economic transition", duration: 0 },
3: { key: 17, id: 0, type: "section", title: "A2. National Income", duration: 0 },
4: { key: 18, id: "5", type: "unit", title: "1. India in the global economy", duration: 0 },
5: { key: 19, id: "6", type: "unit", title: "2. China, India and the rise of Asia", duration: 0 }
};
var keys = Object.keys(data);
var dataArray = [];
var push = true;
var toPush = null;
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var obj = data[key];
switch (obj.type) {
case "section":
if (toPush !== null) {
dataArray.push({ ...toPush });
}
toPush = {};
var titleText = obj.title.split(".")[1].trim();//if there is always a "." in the title string this will clean that up;
toPush.title ? toPush.title += `, ${titleText}` : toPush.title = titleText;
push = true;
break;
case "unit":
push = false;
var contentText = obj.title.split(".")[1].trim();//if there is always a "." in the title string this will clean that up;
toPush.content ? toPush.content += `, ${contentText}` : toPush.content = contentText;
break;
default: break;
}
}
//push the last one
dataArray.push({ ...toPush });
console.log(JSON.stringify(dataArray, null, 2));
//result =>
[
{
"title": "India's Economic Development",
"content": "India as a Developing Economy, Understanding India’s economic transition"
},
{
"title": "National Income",
"content": "India in the global economy, China, India and the rise of Asia"
}
]