简体   繁体   English

如何在多个对象中打断单个JSON object

[英]How to break a single JSON object in multiple objects

I am new to this please excuse me if I make any mistake.我是新手,如果我有任何错误,请原谅。

I am reading a single row of data from database, the data which I receive is JSON object and looks like this:我正在从数据库中读取一行数据,我收到的数据是 JSON object,如下所示:

var details = {jan: '2591.00', feb: '4898.00', mar: '26290.00', apr: '22719.00', may: '26528.00',start: '2020', end: '2022', v1: '34', v2: '22'};

Now I want to alter/modify this JSON object so it looks like this:现在我想改变/修改这个 JSON object 所以它看起来像这样:

var details = [
{jan: '2591.00',start: '2020', end: '2022', v1: '34', v2: '22'},
{feb: '4898.00',start: '2020', end: '2022', v1: '34', v2: '22'},
{mar: '26290.00',start: '2020', end: '2022', v1: '34', v2: '22'},
{apr: '22719.00',start: '2020', end: '2022', v1: '34', v2: '22'},
{may: '26528.00',start: '2020', end: '2022', v1: '34', v2: '22'}

]

MY Approach: I have been trying to achieve this by using:我的方法:我一直在尝试通过使用以下方法来实现这一点:

result = Object
        .keys(details)
        .map(k => ({ [k]: details[k] }));

However, it will break at each key-value pair.但是,它会在每个键值对处中断。

 var details = {jan: '2591.00', feb: '4898.00', mar: '26290.00', apr: '22719.00', may: '26528.00',start: '2020', end: '2022', v1: '34', v2: '22'}; var details = [ {jan: '2591.00',start: '2020', end: '2022', v1: '34', v2: '22'}, {feb: '4898.00',start: '2020', end: '2022', v1: '34', v2: '22'}, {mar: '26290.00',start: '2020', end: '2022', v1: '34', v2: '22'}, {apr: '22719.00',start: '2020', end: '2022', v1: '34', v2: '22'}, {may: '26528.00',start: '2020', end: '2022', v1: '34', v2: '22'} ]

 let details = { jan: '2591.00', feb: '4898.00', mar: '26290.00', apr: '22719.00', may: '26528.00', start: '2020', end: '2022', v1: '34', v2: '22' }; let months = ['jan', 'feb', 'mar', 'apr', 'may']; let detailsByMonth = months.map(month => ({ [month]: details[month], start: details.start, end: details.end, v1: details.v1, v2: details.v2 })); console.log(detailsByMonth);

Here is a one liner.这是一个班轮。 First I filter the keys of the object based on what you don't want inside the array (opposite can be done as well).首先,我根据数组中不需要的内容过滤 object 的键(也可以执行相反的操作)。 Then I map them into objects of required type.然后我将它们 map 转换为所需类型的对象。

const mappedDetails = Object.keys(details)
    .filter((i) => !["v1", "v2", "start", "end"].includes(i))
        .map((j) => ({
            [j]: details[j], 
            start: details.start, 
            end: details.end, 
            v1: details.v1, 
            v2: details.v2
        })
    );

try this尝试这个

var arrContinue = ["start", "end", "v1", "v2"];
const newDetails = Object.keys(details)
  .filter(k => (arrContinue.indexOf(k) == -1))
  .map(k => ({
    [k]: details[k],
    start: details.start,
    end: details.end,
    v1: details.v1,
    v2: details.v2,
  }));

Implementation details has been added the code snippet to understand it better.实施细节已添加到代码片段中以更好地理解它。

 // Input object const details = { jan: '2591.00', feb: '4898.00', mar: '26290.00', apr: '22719.00', may: '26528.00', start: '2020', end: '2022', v1: '34', v2: '22' }; // Create an array which contains only month names by using Object.keys() to get the keys array of details object and then filtered out start, end, v1 & v2 via Array.filter() method. const monthsArray = Object.keys(details).filter((element) =>,['start', 'end', 'v1'. 'v2'];includes(element)). // Creating an array which contain objects with the month details by using Array.map() method. const result = monthsArray:map((month) => ({ [month], details[month]: start. details,start: end. details,end: v1. details,v1: v2. details;v2 })). // Final Result. console;log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将具有多个对象的JSON对象转换为一组对象 - How to convert JSON object with multiple objects into a single set of objects 如何在数组中使用reduce方法将具有公共键的多个Json对象转换为单个Json object? - How to use reduce method within an array to convert multiple Json objects with common key into a single Json object? 如何将多个对象数组转换为单个对象 - How to Convert multiple arrays of objects to single object 如何将多个对象的数组合并为单个 object? - How to consolidate an array of multiple objects into single object? 在Java中,如何在单个JSON对象中表示多个(相同类型的)对象 - In Java, How do I represent multiple objects (of same type) in a single JSON object 使用JavaScript中的相同键将多个json对象合并为单个对象 - Merge multiple json objects to single object with same keys in javascript 如何使用 NodeJS 将 2 个 Json 对象组合成一个 Json object? - How to combine 2 Json objects into a single Json object using NodeJS? 将多个对象推入单个对象 - Push multiple objects into single object 如何将对象数组转换为单个JSON文档/对象 - How to convert an Array of objects single JSON Document/Object 如何对单个NodeJS / Jade合并使用多个JSON对象 - How to use multiple JSON objects for single NodeJS/Jade Merge
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM