格式化JSON的最佳方法

时间:2018-07-28 14:32:19

标签: jsx

我得到了一些数据(如下所示的随机示例),我想将其格式化为以下格式(示例与随机示例不同,但格式相同): 编辑:

格式和示例中的数字都不互相代表。

<%@ Application Language="C#" %>

<script runat="server">

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup

}

void Application_End(object sender, EventArgs e) 
{
    //  Code that runs on application shutdown

}

void Application_Error(object sender, EventArgs e) 
{ 
    // Code that runs when an unhandled error occurs

}

void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started

}

void Session_End(object sender, EventArgs e) 
{
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised.

}

    </script>

我只能想到在循环中无效地使用map()。但是,有没有更好的方法可以自动执行此过滤?

export const formatted = [
    {date: "hone: 30},
    {date: "20/6,
  ];

这只是一种学习经验,我只想学习并提供有关如何在JS React中正确格式化给定数据的布局。

1 个答案:

答案 0 :(得分:1)

似乎有一个reduce和一个find。我将汽车/电话定义为单独的对象,以加快速度。让我知道是否有帮助。从您发布的数组表单中获得相同的结果应该不是很困难。让我知道事情是否会帮助您前进还是我错过了什么。

var cars = {
	key: "car",
	doc_count: 26,
	sales_over_time: {
		buckets: [
			{
				key_as_string: "2018-07-20",
				key: 1532088000000,
				doc_count: 1,
				total_sales: {
					value: 3850
				}
			},
			{
				key_as_string: "2018-07-21",
				key: 1532174400000,
				doc_count: 1,
				total_sales: {
					value: 260
				}
			},
			{
				key_as_string: "2018-07-22",
				key: 1532260800000,
				doc_count: 3,
				total_sales: {
					value: 0
				}
			},
			{
				key_as_string: "2018-07-23",
				key: 1532347200000,
				doc_count: 1,
				total_sales: {
					value: 933
				}
			},
			{
				key_as_string: "2018-07-24",
				key: 1532433600000,
				doc_count: 2,
				total_sales: {
					value: 1902
				}
			}
		]
	}
};
var phones = {
	key: "phone",
	doc_count: 26,
	sales_over_time: {
		buckets: [
			{
				key_as_string: "2018-07-20",
				key: 1532088000000,
				doc_count: 1,
				total_sales: {
					value: 4678
				}
			},
			{
				key_as_string: "2018-07-21",
				key: 1532174400000,
				doc_count: 1,
				total_sales: {
					value: 2445
				}
			},
			{
				key_as_string: "2018-07-22",
				key: 1532260800000,
				doc_count: 3,
				total_sales: {
					value: 833
				}
			},
			{
				key_as_string: "2018-07-23",
				key: 1532347200000,
				doc_count: 1,
				total_sales: {
					value: 139
				}
			},
			{
				key_as_string: "2018-07-24",
				key: 1532433600000,
				doc_count: 2,
				total_sales: {
					value: 1102
				}
			}
		]
	}
}

var result = cars.sales_over_time.buckets.reduce((result, value, index, array) => {	
  var phoneObj = phones.sales_over_time.buckets.find(phone => phone.key === value.key)
	result.push({
	date: value.key_as_string,
	car: value.total_sales.value,
	phone: phoneObj ? phoneObj.total_sales.value : 'N/A',
})
	return result
}, [])

console.log(result)