0

I've been working with arrays and what I want is to create a formula that will allow me to loop over an array that contains objects and take keys with different values and turn them into a multi-dimensional array (I need to keep the order of the keys). I'm already getting this array but now I need to add ['n/a', '--'] on every position where the strings are not the same, like this:

var all = [
 {banana: 1, rose: 2, mouse: 9, apple: 5, ana: 4, carl: 'truck'},
 {banana: 1, rock: 58, car: 19, apple: 5, cheese: 3, carl: 'blue'},
 {banana: 1, cheese: 2, red: 14, clue: 89, apple: 5, ana: 8}
];
//expected to get:
var new-arr = [ [["ana", 4], ["n/a", "--"], ["carl", "truck"]],
 ["n/a", "--"], ["cheese", 3], ["carl", "blue"]],
 [["ana", 8], ["cheese", 2], ["n/a", "--"]] ];

So that at the end I cna create a list like this

list1:

  • ana: 4
  • n/a: --
  • carl: truck

list2:

  • n/a: --
  • cheese: 3
  • carl: blue

list2:

  • ana: 8
  • cheese: 2
  • n/a: --

The code is here https://jsbin.com/yedusigara/1/edit?js,console

Did I do something wrong? Is there any way I can do it all in one function?

asked Aug 1, 2016 at 2:12
4
  • @Tibrogargan you are right thank you. but my formula still doesnt seen to work Commented Aug 1, 2016 at 2:24
  • 1
    "I need to keep the order of the keys" - actually keys have no order. Commented Aug 1, 2016 at 2:26
  • I really don't get what you're trying to do with the "n/a". Can you shorten the example so it's a little bit clearer? having a hard time tracking the mismatched stuff Commented Aug 1, 2016 at 2:28
  • @Bergi thats right thats why Im making it a multidimensional array Commented Aug 1, 2016 at 2:28

1 Answer 1

1

Your bug is not shown in the question, but it is in the fiddle, in this portion:

$('.lol').each(function (i, elm) {
similar_keys[i].forEach(function(spec, j){
 similar_keys[j].forEach(function (spec1, j1){
 if(spec[0] != spec1[0] && j != j1){
 similar_keys[i].push(['n/a', '--']);
 }
});

it doesn't make any sense to iterate through similar_keys[j] on the 3rd loop. j is an index of the inner array. This code just makes no sense. You even rely on having the same number of elements in the DOM and objects in your data.

I can only guess at what you are trying to accomplish, but I would modify your original algorithm instead. Maybe this:

function similars(arr) {
var similar_keys = [];
for (var i = 0; i < arr.length; i++) {
 var tempArr = [];
 for (var key in arr[i]) {
 var found = false;
 var count = 0;
 var index = 0;
 for (var j = 0; j < arr.length; j++) {
 if (arr[j].hasOwnProperty(key)) {
 ++count;
 }
 if (i !== j && arr[j][key] === arr[i][key]) {
 found = true;
 break;
 }
 }
 if (!found && count > 1) {
 tempArr.push([key, arr[i][key]]);
 }
 else if (count > 1) {
 tempArr.push(["N/A", arr[i][key]]);
 } 
 }
 similar_keys.push(tempArr);
}
return similar_keys;

}

EDIT: Still not sure what you are looking for. What I think you need to do:

1) Sort each of the rows in similar_keys by the first element 2) Compare first element of each row, and insert the N/A entry to the row with the lowest sort order. 3) continue through each column, inserting as necessary.

i will try to modify your fiddle to demonstrate

EDIT: this should work. https://jsbin.com/nesiqogebo/edit?js,console,output

answered Aug 1, 2016 at 2:31
Sign up to request clarification or add additional context in comments.

3 Comments

thank you I add the last part on the description of the problem. but your sugestion doesn't seen to work. This is what I get: [[["N/A", 1], ["N/A", 5], ["ana", 4], ["carl", "truck"]], [["cheese", 3], ["carl", "blue"]], [["cheese", 2], ["ana", 8]]]
yeah, I don't think I understand your intention very well. I guess you want to pad the finished array.
look at the description I just update it saying what my goald is.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.