2

I have a json like this :

{
    "status": 1,
    "message": "ok",
    "data": [
        {
            "tenor": "23"
        },
        {
            "tenor": "17"
        },
        {
            "tenor": "37"
        },
        {
            "tenor": "27,29"
        },
        {
            "tenor": "33,35"
        }
    ]
}

and I want the result to be something like this:

{ "status": 1, "message": "ok", "data": [ { "tenor": "23" }, { "tenor": "17" }, { "tenor": "37" }, { "tenor": "27" }, { "tenor": "29" }, { "tenor": "33" }, { "tenor": "35" } ] }

What I have tried:

var arrayCoba = [];
var array1 = { "status": 1, "message": "ok", "data": [ { "tenor": "23" }, { "tenor": "17" }, { "tenor": "37" }, { "tenor": "27,29" }, { "tenor": "33,35" } ] }

for(var i = 0; i<array1.data.length; i++){
    var string = array1.data[i].tenor;
    var substring = ",";

    if(string.includes(substring) == true){
        var tenor = array.data[i].tenor;
        var tenorArr = tenor.split(',');
        var dataTenor = tenorArr.map(tenor => ({ tenor }));
        arrayCoba.push(dataTenor);
    }
 }

 var dataHasil = array1.data.concat(arrayCoba);

 return res.json({status:1,message:'ok',data:dataHasil}); 

but the result that i get is :

{
    "status": 1,
    "message": "ok",
    "data": [
        {
            "tenor": "23"
        },
        {
            "tenor": "17"
        },
        {
            "tenor": "37"
        },
        {
            "tenor": "27,29"
        },
        {
            "tenor": "33,35"
        },
        [
            {
                "tenor": "27"
            },
            {
                "tenor": "29"
            }
        ],
        [
            {
                "tenor": "33"
            },
            {
                "tenor": "35"
            }
        ]
    ]
}

can anyone help me? Thank you ..

4 Answers 4

2

var dataTenor = tenorArr.map(tenor => ({ tenor })); return an array of tenor object. arrayCoba.push(dataTenor); add the array in other array and is for this you get an array in array.

use spread operator -> arrayCoba.push(...dataTenor);

for (var i = 0; i < array1.data.length; i++) {
    var string = array1.data[i].tenor;
    var substring = ",";

    if (string.includes(substring) == true) {
        var tenor = array1.data[i].tenor;
        var tenorArr = tenor.split(',');
        var dataTenor = tenorArr.map(tenor => ({
            tenor
        }));
        arrayCoba.push(...dataTenor);
    } else {
        arrayCoba.push(array1.data[i]);
    }
}

var dataHasil = arrayCoba;

UPDATE: a another way with map operator.

var substring = ',';

var copyDataTenors = array1.data;
var arrayCoba = "".concat(copyDataTenors
    .map(obj => obj.tenor))
    .split(substring)
    .map(tenorString => ({
        'tenor': tenorString
    }))

var dataHasil = arrayCoba;

UPDATE: With recursion and more generic than previous solutions

const data = [{
        "tenor": "23"
    },
    {
        "tenor": "17"
    },
    {
        "tenor": "37"
    },
    {
        "tenor": "27,29"
    },
    {
        "tenor": "33,35"
    }
]

/**
 *
 * @param {*} fn function callback
 * @param {*} v string[] or string
 * @param {*} attr string
 * @param {*} substring string
 */
const processDeepObject = (fn, v, attr, substring) => {
    return typeof v === 'string' ? {
        [attr]: v
    } : v[attr] ? fn(v[attr].split(substring), attr, substring) : [];
}

/**
 * Version 1
 * @param {*} arr Array of objects
 * @param {*} attr target you want keep (for this use case tenor)
 * @param {*} substring which char to split the data (for this use case ',')
 */
const deepObjectFlatten = (arr, attr, substring) => [].concat(...arr.map((v) => processDeepObject(deepObjectFlatten, v, attr, substring)));



/**
 * Version 2 same as version 1 but compact
 * @param {*} arr Array of objects
 * @param {*} attr target you want keep (for this use case tenor)
 * @param {*} substring which char to split the data (for this use case ',')
 */
const deepObjectFlattenV2 = (arr, attr, substring) => [].concat(...arr.map((v) => {
    return typeof v === 'string' ? {
        [attr]: v
    } : v[attr] ? deepObjectFlattenV2(v[attr].split(substring), attr, substring) : [];
}));


console.log(deepObjectFlatten(data, 'tenor', ','))
console.log(deepObjectFlattenV2(data, 'tenor', ','))
/*
Output :
//v1
[ { tenor: '23' },
  { tenor: '17' },
  { tenor: '37' },
  { tenor: '27' },
  { tenor: '29' },
  { tenor: '33' },
  { tenor: '35' } ]

//v2
  [ { tenor: '23' },
  { tenor: '17' },
  { tenor: '37' },
  { tenor: '27' },
  { tenor: '29' },
  { tenor: '33' },
  { tenor: '35' } ]
 */
Sign up to request clarification or add additional context in comments.

1 Comment

My pleasure :) .
1

You were using the results returned by map in a wrong way,you can use spread operator as mentioned by John to fix this.

You can do something like this:

var arrayCoba = [];
var array1 = { "status": 1, "message": "ok", "data": [ { "tenor": "23" }, { "tenor": "17" }, { "tenor": "37" }, { "tenor": "27,29" }, { "tenor": "33,35" } ] }

for(var i = 0; i<array1.data.length; i++){
    var string = array1.data[i].tenor;
    var substring = ",";

    if(string.includes(substring) == true){
        var tenor = array1.data[i].tenor;
        var tenorArr = tenor.split(',');
        var dataTenor = tenorArr.map(tenor => ({ "tenor" : tenor }));
        arrayCoba.push(...dataTenor);
    } else {
        arrayCoba.push({"tenor": array1.data[i].tenor})
    }

 }
 return res.json({status:1,message:'ok',data : arrayCoba}); 

John,dacost has used almost the same logic, didn't see it earlier, so have posted this.

Comments

1

You can do it like below:

function test() {
    let obj = { "status": 1, "message": "ok", "data": [ { "tenor": "23" }, { "tenor": "17" }, { "tenor": "37" }, { "tenor": "27,29" }, { "tenor": "33,35" } ] }
    let res = [];
    obj.data
        .map(item => item.tenor.split(','))
        .forEach(tenors => {
            tenors.forEach(tenor => res.push({tenor}));
        });

    obj.data = res;
    return obj;
}

After invoking above method it returns:

{"status":1,"message":"ok","data":[{"tenor":"23"},{"tenor":"17"},{"tenor":"37"},{"tenor":"27"},{"tenor":"29"},{"tenor":"33"},{"tenor":"35"}]}

Comments

-2

you get result from mongodb after result put into JSON.stringify function than get result like that "{"name":5,"value":6}"

2 Comments

can you explain this?
Can you give us an example of you purpose ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.