1

For the following

array1 = [{
        "x": 0,
        "y": 1,
        "z": 1,
        "i": "chart1"
         }, {
        "x": 0,
        "y": 2,
        "z": 1,
        "i": "chart2"
         }, {
        "x": 1,
        "y": 1,
        "z": 1,
        "i": "chart3 "
    }
]

array2 = [{
        "x": 1,
        "y": 1,
        "z": 1,
        "i": "chart1"
        }, {
        "x": 0,
        "y": 1,
        "z": 1,
        "i": "chart2"
        }, {
        "x": 0,
        "y": 2,
        "z": 1,
        "i": "chart3"
    }
]

compare x and y of array1 and array2 and find the position if it is equal and return the set of array of i value.

i.e in above case it should return:

array3=["chart2","chart3","chart1"]

I have a json as below:

json = [{
        "visType:" bar "," visName ":" chart1 "},{" visType: "bar",
        "visName": "chart2"
    }, {
        "visType:" Pie "," visName ":" chart3 "}]

And this need to be sort based on array3 = ["chart2","chart3","chart1"] the output should be as in updated json

updatedjson = [{
        "visType:" bar "," visName ":" chart2 "},{" visType: "Pie",
        "visName": "chart3"
    }, {
        "visType:" bar "," visName ":" chart1 "}]

I need a solution using lodash or javascript.

3
  • 1
    Before anything, reformat the code in this question. Commented Mar 7, 2018 at 20:47
  • what you have tried please share the codebase we will look into it. thanks Commented Mar 7, 2018 at 20:48
  • 2
    Sounds like homework to me. Commented Mar 7, 2018 at 20:48

1 Answer 1

1
const array3 = [...array1, ...array2].filter(({x,y}) => x===y);
const updatedjson = array3.map(({i}) => json.find(({visName}) => i === visName));

Edit: Fixed the input data which was full of syntax errors:

var array1 = [{ "x": 0, "y": 1, "z": 1, "i": "chart1" }, { "x": 0, "y": 2, "z": 1, "i": "chart2" }, { "x": 1, "y": 1, "z": 1, "i": "chart3" }];
var array2 = [{ "x": 1, "y": 1, "z": 1, "i": "chart1" }, { "x": 0, "y": 1, "z": 1, "i": "chart2" }, { "x": 0, "y": 2, "z": 1, "i": "chart3" }];

var json = [
    { "visType": "bar", "visName": "chart1" },
    { "visType": "bar", "visName": "chart2" },
    { "visType": "Pie", "visName": "chart3" }
];

const array3 = [...array1, ...array2].filter(({ x, y }) => x === y);
const updatedjson = array3.map(({ i }) => json.find(({ visName }) => i === visName));
console.log(array3);
console.log(updatedjson);
Sign up to request clarification or add additional context in comments.

3 Comments

Did i pass the class? xD
No the answer is not matching :)
Yes it does. Your input data is all messed up & full of syntax errors like missing quotes. Once you fix those, my code works ;)

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.