0

I have 2 arrays in the below:

sonuc_X = [ 41.01766, 41.01746, 41.05877, 41.05974, 41.04383, 41.03693 ];
var labels = ["lat"];

I also have merge processing below:

var obj_X = {};
    for (var j = 0; j < labels.length; j++) {
        obj_X[labels[j]] = sonuc_X[j];
    }
var asJSON = JSON.stringify(obj_X);
    console.log(asJSON);

when I combine two sequences at the json, the following result comes out:

{"lat":41.01766}

I expect:

[ "lat":41.01766, "lat":41.01746, "lat":41.05877, "lat":41.05974, "lat":41.04383, "lat":41.03693 ]

?

3
  • What's the expected result? Commented Mar 11, 2018 at 14:05
  • How do you want to merge those arrays? Commented Mar 11, 2018 at 14:07
  • the result is not valid. Commented Mar 11, 2018 at 14:11

1 Answer 1

1

You could iterate the data and build new objects with the wanted keys.

var data = [41.01766, 41.01746, 41.05877, 41.05974, 41.04383, 41.03693],
    keys = ["lat", "lng"],
    result = data.reduce(function (r, v, i) {
        if (i % keys.length === 0) {
            r.push({});
        }
        r[r.length - 1][keys[i % keys.length]] = v;
        return r;
    }, []);
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

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.