简体   繁体   中英

Create a couple of new arrays from multiple (3) arrays

I am trying to work some data from 3 arrays I have.

obj = [["TestObj","1","value1","1"],["TestObj2","2","value2","1"],["TestObj3","3","value3",["1","2"]]];
//[Name, ID, value, Group ID(s)] //TestObj3 belongs to both groups
groups = [["TestGrp", "1"],["TestGrp2", "2"]];
//[Group Name, Group ID]
lines = [["valueX, valueZ","valueY, valueD"],["OBJ(1)","OBJ(2),OBJ(3)"], ["GRP(1)","GRP(2)"]];

Now ultimately, I want to create two new arrays.

Looking at lines[], I want to look at each OBJ entry and replace it with the corresponding value from obj[]. For example looking at OBJ(1), 1 is the ID of OBJ and it would correspond to the TestObj basically:

lines[1][0] = obj[0][2]

Very similar to OBJ, the GRP(1) needs to be looked up in groups[] and then look into corresponding Group ID in obj[], and then replace GRP(1) with the values of OBJs found in the group. So the goal is to end up with 2 arrays, something like this:

lines = [["valueX, valueZ","valueY, valueD"],["value1","value2, value3"], ["value1, value2, value3","value3"]];

newObj = [["OBJ(1)", "value1", 1, "TestGrp"], ["OBJ(2)", "value2", 1, "TestGrp"], ["OBJ(3)", "value3", [1, 2], [TestGrp, TestGrp2]]];
//[[Name, value, Group ID(s), Group Name(s)]]

What would be the easiest way of achieving something like this? I've been trying with a lot of loops but it just makes things broken and massively complicated.

Cheers

You can't get the result without using loops or maps/reducers. I implemented the below solution in vanilla javascript, if you're using any external library, like jquery on client-side or lodash on server-side, then you can try to simplify the code using them.

It'll be very hard to explain the entire code, that's why I've added the sample values in the form of comments after performing each operation. They'll help you in understanding the code.

Also, I haven't used the best practices a person should follow while writing code like not so descriptive variable names, etc. I tried more on getting the result. So, I hope you'll do justice to the code once you verify its working.

 let obj = [ ["TestObj","1","value1","1"], ["TestObj2","2","value2","1"], ["TestObj3","3","value3", ["1","2"]] ]; let groups = [ ["TestGrp", "1"], ["TestGrp2", "2"] ]; let lines = [ ["valueX, valueZ","valueY, valueD"], ["OBJ(1)","OBJ(2),OBJ(3)"], ["GRP(1)","GRP(2)"] ]; const getId = (el => el.trim().replace(/[^0-9]/g, '')); const getGroupName = (grpId => groups.find(grp => (grp[1] === grpId))[0]); let newLines = [[...lines[0]]]; newLines[1] = lines[1].map(line => { // line = 'OBJ(2),OBJ(3)' let lineArr = line.split(','); // lineArr = [ 'OBJ(2)', 'OBJ(3)' ] let value; let valuesArr = lineArr.map(el => { let idToSearch = getId(el) // idToSearch = '2' obj.some(objEl => { // objEl = ['TestObj2','2','value2','1'] if(objEl[1] === idToSearch) { value = objEl[2]; return true; } }); return value; // return 'value2' }); return valuesArr.join(', '); }); newLines[2] = lines[2].map(line => { // line = 'GRP(1)' let grpId = getId(line) // grpId = '1' let valuesArr = obj.map(objEl => { if(Array.isArray(objEl[3])) { // objEl[3] = ['TestObj3','3','value3', ['1','2']] let value; // Assuming single occurence of ids in objEl[3] objEl[3].some(objElGrpId => { if(grpId === objElGrpId) { value = objEl[2]; return true; } }); return value; // return 'value3' } else { if(grpId === objEl[3]) { return objEl[2]; } } }); return valuesArr.filter(Boolean).join(', '); }); let newObj = []; lines[1].forEach(line => { // line = 'OBJ(2),OBJ(3)' let lineArr = line.split(','); // lineArr = [ 'OBJ(2)', 'OBJ(3)' ] let finalArr = lineArr.map(el => { let currentIdArr = []; currentIdArr.push(el); // finalArr = [ 'OBJ(2)' ] let idToSearch = getId(el) // idToSearch = '2' obj.some(objEl => { if(objEl[1] === idToSearch) { currentIdArr.push(objEl[2]); // finalArr = [ 'OBJ(2)', 'value2' ] if(Array.isArray(objEl[3])) { currentIdArr.push(objEl[3].map(grpId => parseInt(grpId))); currentIdArr.push(objEl[3].map(grpId => getGroupName(grpId))); } else { currentIdArr.push(parseInt(objEl[3])); // finalArr = [ 'OBJ(2)', 'value2', 1 ] currentIdArr.push(getGroupName(objEl[3])); // finalArr = [ 'OBJ(2)', 'value2', 1, 'TestGrp2' ] } return true; } }); return currentIdArr; }); finalArr.map(arr => newObj.push(arr)); }); console.log('newLines', newLines); console.log('newObj', newObj);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM