D
D
domanskiy2019-01-10 16:08:42
JSON
domanskiy, 2019-01-10 16:08:42

How to sort one JSON list by another in JS and get the third one?

Initially, there are two XMLs, I convert them to JSON. in my version, JS (ECMAScript) has no DOM and no way to parse XML. But there is support for JSON and JSON.parse
There are two JSON lists.
1st JSON

{"JOB": {"Inks": {"ink": [
    {
        "Angle": 7,
        "Frequency": 150,
        "InkParam": "новая",
        "ID": 1,
        "ColorName": "PANTONE  877 C"
    },
    {
        "Angle": 67,
        "Frequency": 150,
        "InkParam": "новая",
        "ID": 2,
        "ColorName": "PANTONE Bright Red C"
    },
    {
        "Angle": 82,
        "Frequency": 175,
        "InkParam": "новая",
        "ID": 3,
        "ColorName": "Yellow"
    },
    {
        "Angle": 37,
        "Frequency": 175,
        "InkParam": "новый",
        "ID": 4,
        "ColorName": "Black"
    },
    {
        "Angle": 37,
        "Frequency": 150,
        "InkParam": "новый",
        "ID": 5,
        "ColorName": "PANTONE  427 C"
    },
    {
        "Angle": 37,
        "Frequency": 150,
        "InkParam": "новый",
        "ID": 6,
        "ColorName": "Cyan"
    },
    {
        "Angle": 7,
        "Frequency": 150,
        "InkParam": "новая",
        "ID": 7,
        "ColorName": "Magenta"
    },
    {
        "Angle": 37,
        "Frequency": 175,
        "InkParam": "новая",
        "ID": 8,
        "ColorName": "PANTONE 581 C"
    },
    {
        "Angle": 50,
        "Frequency": 133,
        "InkParam": 180202,
        "ID": 9,
        "ColorName": "PANTONE 7739 C"
    },
    {
        "Angle": 50,
        "Frequency": 150,
        "InkParam": 180201,
        "ID": 10,
        "ColorName": "PANTONE Blue 072 C"
    }
]}}}

2nd JSON
{"JOB": {"Inks": {"Ink": [
    {
        "ID": 1,
        "ColorName": "Cyan"
    },
    {
        "ID": 2,
        "ColorName": "Magenta"
    },
    {
        "ID": 3,
        "ColorName": "Yellow"
    },
    {
        "ID": 4,
        "ColorName": "Black"
    },
    {
        "ID": 5,
        "ColorName": "PANTONE 427 C"
    },
    {
        "ID": 6,
        "ColorName": "PANTONE 877 C"
    },
    {
        "ID": 7,
        "ColorName": "PANTONE Bright Red C"
    },
    {
        "ID": 8,
        "ColorName": "PANTONE 7739 C"
    },
    {
        "ID": 9,
        "ColorName": "PANTONE 581 C"
    },
    {
        "ID": 10,
        "ColorName": "PANTONE Blue 072 C"
    }
]}}}

It is necessary to insert from the first list into the second InkParam. But both lists are sorted/have a different order in different ways. Only ColorName matches.
At the output you need to get:
{"JOB": {"Inks": {"Ink": [
    {
        "ID": 1,
        "InkParam": "новый",
        "ColorName": "Cyan"
    },
    {
        "ID": 2,
        "InkParam": "новая",
        "ColorName": "Magenta"
    },
    {
        "ID": 3,
        "InkParam": "новая",
        "ColorName": "Yellow"
    },
    {
        "ID": 4,
        "InkParam": "новый",
        "ColorName": "Black"
    },
    {
        "ID": 5,
        "InkParam": "новый",
        "ColorName": "PANTONE 427 C"
    },
    {
        "ID": 6,
        "InkParam": "новая",
        "ColorName": "PANTONE 877 C"
    },
    {
        "ID": 7,
        "InkParam": "новая",
        "ColorName": "PANTONE Bright Red C"
    },
    {
        "ID": 8,
        "InkParam": 180202,
        "ColorName": "PANTONE 7739 C"
    },
    {
        "ID": 9,
        "InkParam": "новая",
        "ColorName": "PANTONE 581 C"
    },
    {
        "ID": 10,
        "InkParam": 180201,
        "ColorName": "PANTONE Blue 072 C"
    }
]}}}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
domanskiy, 2019-01-11
@domanskiy

Convert to objects. Those. bring to mind

var tmp = {
        "Angle": 7,
        "Frequency": 150,
        "InkParam": "новая",
        "ID": 1,
        "ColorName": "PANTONE  877 C"
    },
    {
        "Angle": 67,
        "Frequency": 150,
        "InkParam": "новая",
        "ID": 2,
        "ColorName": "PANTONE Bright Red C"
    },
    {
        "Angle": 82,
        "Frequency": 175,
        "InkParam": "новая",
        "ID": 3,
        "ColorName": "Yellow"
    };

on let gives an error.
arr2 is the second JSON?
If it's not difficult, can you write a ready-made version in JS?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question