将javascript对象数组转换为JSON
我有一组以下格式的数据:将javascript对象数组转换为JSON,javascript,jquery,arrays,json,object,Javascript,Jquery,Arrays,Json,Object,我有一组以下格式的数据: 我需要将其转换为JSON格式,如下所示: { “movietitle1”:{“id”:445,“发行”:“1985年6月16日”}, “movietitle2”:{“id”:487,“发行”:“1993年8月12日”} } 我不知道如何做到这一点。你可以通过一些基本的数据操作来做到这一点 将数据存储在变量中,我们将其称为input\u data // create an object to store the newly formatted data var outp
我需要将其转换为JSON格式,如下所示:
{
“movietitle1”:{“id”:445,“发行”:“1985年6月16日”},
“movietitle2”:{“id”:487,“发行”:“1993年8月12日”}
}
我不知道如何做到这一点。你可以通过一些基本的数据操作来做到这一点
将数据存储在变量中,我们将其称为input\u data
// create an object to store the newly formatted data
var output_data = {};
// the code in here is run once for each item in input_data
for (var i = 0; i < input_data.length; i++) {
// get the current item's title
var title = input_data[i].title;
// use the title as the key in the output data
// and assign the other values to that key
output_data[title] = {
id: input_data[i].id,
release: input_data[i].release
};
}
// use JSON.stringify() to make a valid JSON string
var json = JSON.stringify(output_data);
// now use the variable json which contains your JSON string
循环遍历您的数据并使用每个条目构建另一个变量,让我们调用它output\u data
// create an object to store the newly formatted data
var output_data = {};
// the code in here is run once for each item in input_data
for (var i = 0; i < input_data.length; i++) {
// get the current item's title
var title = input_data[i].title;
// use the title as the key in the output data
// and assign the other values to that key
output_data[title] = {
id: input_data[i].id,
release: input_data[i].release
};
}
// use JSON.stringify() to make a valid JSON string
var json = JSON.stringify(output_data);
// now use the variable json which contains your JSON string
//创建一个对象来存储新格式化的数据
变量输出_data={};
//这里的代码针对输入数据中的每个项目运行一次
对于(变量i=0;i
您可以使用和一些基本数据操作来完成此操作
将数据存储在变量中,我们将其称为input\u data
// create an object to store the newly formatted data
var output_data = {};
// the code in here is run once for each item in input_data
for (var i = 0; i < input_data.length; i++) {
// get the current item's title
var title = input_data[i].title;
// use the title as the key in the output data
// and assign the other values to that key
output_data[title] = {
id: input_data[i].id,
release: input_data[i].release
};
}
// use JSON.stringify() to make a valid JSON string
var json = JSON.stringify(output_data);
// now use the variable json which contains your JSON string
循环遍历您的数据并使用每个条目构建另一个变量,让我们调用它output\u data
// create an object to store the newly formatted data
var output_data = {};
// the code in here is run once for each item in input_data
for (var i = 0; i < input_data.length; i++) {
// get the current item's title
var title = input_data[i].title;
// use the title as the key in the output data
// and assign the other values to that key
output_data[title] = {
id: input_data[i].id,
release: input_data[i].release
};
}
// use JSON.stringify() to make a valid JSON string
var json = JSON.stringify(output_data);
// now use the variable json which contains your JSON string
//创建一个对象来存储新格式化的数据
变量输出_data={};
//这里的代码针对输入数据中的每个项目运行一次
对于(变量i=0;i
您可以使用和一些基本数据操作来完成此操作
将数据存储在变量中,我们将其称为input\u data
// create an object to store the newly formatted data
var output_data = {};
// the code in here is run once for each item in input_data
for (var i = 0; i < input_data.length; i++) {
// get the current item's title
var title = input_data[i].title;
// use the title as the key in the output data
// and assign the other values to that key
output_data[title] = {
id: input_data[i].id,
release: input_data[i].release
};
}
// use JSON.stringify() to make a valid JSON string
var json = JSON.stringify(output_data);
// now use the variable json which contains your JSON string
循环遍历您的数据并使用每个条目构建另一个变量,让我们调用它output\u data
// create an object to store the newly formatted data
var output_data = {};
// the code in here is run once for each item in input_data
for (var i = 0; i < input_data.length; i++) {
// get the current item's title
var title = input_data[i].title;
// use the title as the key in the output data
// and assign the other values to that key
output_data[title] = {
id: input_data[i].id,
release: input_data[i].release
};
}
// use JSON.stringify() to make a valid JSON string
var json = JSON.stringify(output_data);
// now use the variable json which contains your JSON string
//创建一个对象来存储新格式化的数据
变量输出_data={};
//这里的代码针对输入数据中的每个项目运行一次
对于(变量i=0;i
您可以使用和一些基本数据操作来完成此操作
将数据存储在变量中,我们将其称为input\u data
// create an object to store the newly formatted data
var output_data = {};
// the code in here is run once for each item in input_data
for (var i = 0; i < input_data.length; i++) {
// get the current item's title
var title = input_data[i].title;
// use the title as the key in the output data
// and assign the other values to that key
output_data[title] = {
id: input_data[i].id,
release: input_data[i].release
};
}
// use JSON.stringify() to make a valid JSON string
var json = JSON.stringify(output_data);
// now use the variable json which contains your JSON string
循环遍历您的数据并使用每个条目构建另一个变量,让我们调用它output\u data
// create an object to store the newly formatted data
var output_data = {};
// the code in here is run once for each item in input_data
for (var i = 0; i < input_data.length; i++) {
// get the current item's title
var title = input_data[i].title;
// use the title as the key in the output data
// and assign the other values to that key
output_data[title] = {
id: input_data[i].id,
release: input_data[i].release
};
}
// use JSON.stringify() to make a valid JSON string
var json = JSON.stringify(output_data);
// now use the variable json which contains your JSON string
//创建一个对象来存储新格式化的数据
变量输出_data={};
//这里的代码针对输入数据中的每个项目运行一次
对于(变量i=0;i
您的混合术语。我假设您询问的是如何使用JavaScript对象处理数据,而不是使用JSON数据处理字符串。(后者可以用JSON.parse
转换)
首先迭代数组并分配给对象。这种数据操作在使用时效果很好,请查看
在vanilla JS中,让我们尝试以下内容:
var newData = {};
data.forEach(function(item) {
var title = item.title;
delete item.title;
newData[title] = item;
});
有点粗糙,但能完成任务
我个人会用这个:
你的混合术语。我假设您询问的是如何使用JavaScript对象处理数据,而不是使用JSON数据处理字符串。(后者可以用JSON.parse
转换)
首先迭代数组并分配给对象。这种数据操作在使用时效果很好,请查看
在vanilla JS中,让我们尝试以下内容:
var newData = {};
data.forEach(function(item) {
var title = item.title;
delete item.title;
newData[title] = item;
});
有点粗糙,但能完成任务
我个人会用这个:
你的混合术语。我假设您询问的是如何使用JavaScript对象处理数据,而不是使用JSON数据处理字符串。(后者可以用JSON.parse
转换)
首先迭代数组并分配给对象。这种数据操作在使用时效果很好,请查看
在vanilla JS中,让我们尝试以下内容:
var newData = {};
data.forEach(function(item) {
var title = item.title;
delete item.title;
newData[title] = item;
});
有点粗糙,但能完成任务
我个人会用这个:
你的混合术语。我假设您询问的是如何使用JavaScript对象处理数据,而不是使用JSON数据处理字符串。(后者可以用JSON.parse
转换)
首先迭代数组并分配给对象。这种数据操作在使用时效果很好,请查看
在vanilla JS中,让我们尝试以下内容:
var newData = {};
data.forEach(function(item) {
var title = item.title;
delete item.title;
newData[title] = item;
});
有点粗糙,但能完成任务
我个人会用这个:
您需要的是将一个数组转换成一个映射,通过一个特定的属性进行键控,如果您真的需要JSON,您可以在生成的JS对象上调用JSON.stringify
请参见您要求的是将数组转换为映射,并按特定属性键入,