0

i want to create jSon format from this string value:

hello|world|this|is|javascript || my|name|is|mahdi

in created jSon array from that i can not wrap this string to separator as 2 array by split with ||. my code is work fine but i can not create some array in json after split string with ||.

result my code is:

{
    "FILEDS": [
        {
            "template_id": "123",
            "fields_id": "456"
        },
        {
            "item": "hello"
        },
        {
            "item": "world"
        },
        {
            "item": "this"
        },
        {
            "item": "is"
        },
        {
            "item": "javascript "
        },
        {
            "item": " my"
        },
        {
            "item": "name"
        },
        {
            "item": "is"
        },
        {
            "item": "mahdi"
        }
    ]
}

but i want to have this result like with below json format:

{
    "FILEDS": [
        {
            "template_id": "123",
            "fields_id": "456"
        },
        [
            {
                "item": "hello"
            },
            {
                "item": "world"
            },
            {
                "item": "this"
            },
            {
                "item": "is"
            },
            {
                "item": "javascript "
            }
        ],
        [
            {
                "item": " my"
            },
            {
                "item": "name"
            },
            {
                "item": "is"
            },
            {
                "item": "mahdi"
            }
        ]
    ]
}

My code is below code and how to create this array in each for for some data to create and wrap to []?

<script type="text/javascript" language="JavaScript">

    var data = "hello|world|this|is|javascript || my|name|is|mahdi";
    var templates = {
        FILEDS: []
    };

    templates.FILEDS.push({
        "template_id": "123",
        "fields_id": "456",
    });

    var split_data = data.split("||");

    for (var i = 0; i < split_data.length; i++) {
        var fields = split_data[i].split("|");
        for (var j = 0; j < fields.length; j++) {
            templates.FILEDS.push({
                "item": fields[j],
            });
        }
    }
    console.log(JSON.stringify(templates));
</script>
0

3 Answers 3

2

Try this;

var split_data = data.split("||");

for (var i = 0; i < split_data.length; i++) {
    var sentence = [];
    var fields = split_data[i].split("|");
    for (var j = 0; j < fields.length; j++) {
        sentence.push({
            "item": fields[j],
        });
    }
    templates.FILEDS.push(sentence)
}
Sign up to request clarification or add additional context in comments.

Comments

2

You'll need a secondary array to hold the data.

var data = "hello|world|this|is|javascript || my|name|is|mahdi";
var templates = {
    FILEDS: []
};

templates.FILEDS.push({
    "template_id": "123",
    "fields_id": "456",
});

var split_data = data.split("||");

for (var i = 0; i < split_data.length; i++) {
    var fields = split_data[i].split("|");
    var arr = [];
    for (var j = 0; j < fields.length; j++) {
        arr.push({ "item" : fields[j] });
    }
    templates.FILEDS.push(arr);
}
console.log(JSON.stringify(templates));
// result" {"FILEDS":[{"template_id":"123","fields_id":"456"},[{"item":"hello"},{"item":"world"},{"item":"this"},{"item":"is"},{"item":"javascript "}],[{"item":" my"},{"item":"name"},{"item":"is"},{"item":"mahdi"}]]}

Comments

0

Yet another variant with Array.map

[].push.apply(templates.FILEDS, data.split("||").map(function(el){
    return el.split('|').map(function(item){
        return { "item" : item };
    })
}));

UPDATE if you want objects like

{ "FILEDS": [ 
    { "template_id": "123", "fields_id": "456" }, 
    {"items": [
        [ "hello", "world", "this", "is", "javascript " ], 
        [ " my", "name", "is", "mahdi" ] 
    ] }
]}

you can a bit change code above

templates.FILEDS.push({
    "item" : data.split("||").map(function(el){
                 return el.split('|');
             })
    }
);

6 Comments

@mahdipishguy, note that other answers do the same, and can be more effective (faster) on big arrays, because simple for in most cases faster then map
use Array.prototype.push.apply instead of [].push.apply
@Sagi, can you explain why? ;-) Array.prototype.push === [].push // true
For me its more elegant, because you don't need to create an instance of array just to use the push function.
@Grundy how to assign key for nested array? like with this: { "FILEDS": [ { "template_id": "123", "fields_id": "456" }, "items":[ "hello", "world", "this", "is", "javascript " ], [ " my", "name", "is", "mahdi" ] ] }
|

Your Answer

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