How to replace a string with an attribute in json.

has a string like this.
ouabc;ouweix;oualex;5;oumyhome;1;
wants to replace the number in it with the value in the following json (match the value of id and take the id in all the children under the id). Like this: ouabc;ouweix;oualex;ou9QFwjzTVc;ou9QFwk1I4q;ou9QFwh0aqC;ou9QFwp-LV;oumyhome;ou9QFwijJ4;ou9QFwoPJh;


{
    "data": [
        {
            "id": "1",
            "deptname": "",
            "children": [
                {
                    "id": "ou9QFwijJ4",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwoPJh",
                    "deptname": "",
                    "children": []
                }
            ]
        },
        {
            "id": "5",
            "deptname": "",
            "children": [
                {
                    "id": "ou9QFwjzTVc",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwk1I4q",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwh0aqC",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwp-LV",
                    "deptname": "",
                    "children": []
                }
            ]
        }
    ]
}
Apr.26,2022

var obj = {
    "data": [
        {
            "id": "1",
            "deptname": "",
            "children": [
                {
                    "id": "ou9QFwijJ4",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwoPJh",
                    "deptname": "",
                    "children": []
                }
            ]
        },
        {
            "id": "5",
            "deptname": "",
            "children": [
                {
                    "id": "ou9QFwjzTVc",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwk1I4q",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwh0aqC",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwp-LV",
                    "deptname": "",
                    "children": []
                }
            ]
        }
    ]
}
var str = "ouabc;ouweix;oualex;5;oumyhome;1;";

function setStr(str) {
  var arr = str.split(';');
  var newObj = {};
  obj.data.forEach(item => {
    newObj[item.id] = item.children.map(_item => _item.id);
  })
  Object.keys(newObj).forEach(item => {
    arr.splice(arr.indexOf(item), 1, newObj[item])
  })
  return arr.join(';');
}
console.log(setStr(str));

var obj = {
    "data": [
        {
            "id": "1",
            "deptname": "",
            "children": [
                {
                    "id": "ou9QFwijJ4",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwoPJh",
                    "deptname": "",
                    "children": []
                }
            ]
        },
        {
            "id": "5",
            "deptname": "",
            "children": [
                {
                    "id": "ou9QFwjzTVc",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwk1I4q",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwh0aqC",
                    "deptname": "",
                    "children": []
                },
                {
                    "id": "ou9QFwp-LV",
                    "deptname": "",
                    "children": []
                }
            ]
        }
    ]
};
var str = "ouabc;ouweix;oualex;5;oumyhome;1;";
var ret = str.replace(/[0-9]+/g, function(s) {
    var item = obj.data.find(function(n) {
        return +n.id === +s;
    });
    return (function getChildrenId(o) {
        var tmp, ref = "";
        if (Array.isArray(o.children) && o.children.length) {
            ref = o.children.reduce(function(r, n) {
                r.push(n.id);
                (tmp = getChildrenId(n)) && r.push(tmp);
                return r;
            }, []).join(";");
        }
        return ref;
    })(item);
});
console.log(ret);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b9ea89-2e165.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b9ea89-2e165.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?