Karthikeyan
Karthikeyan

Reputation: 2001

javascript array - comparing elements with other elements within the same array

Am comparing javascript array of elements with other elements within the same array. If elements are same means, i have to give same number for all the common elements. If the elements are different i have to give some different number for all the non identical elements in another element.

for example :

Array structure = [{Location, Date, Number}]


array = [{ 'LA','2017-12-01',1},
         { 'LA','2017-12-01',1},
         { 'NY','2017-12-01',2},
         { 'NY','2016-10-01',3},
         { 'LA','2017-12-01',1},
         { 'LA','2017-12-01',1},
         { 'LA','2017-12-01',1}]

In this array 'Number' is dynamic element, It should be populate on the following rules.

key1 = location + '-' +date;

Consider Key1 is the first element ( combination of location + date ). If the same key1 is present in the array, then 'Number' is common for all the same Key1.

In the above example {'LA','2017-12-01',1 } having the same number 1.

{ 'NY','2017-12-01',2} having the number 2. and { 'NY','2016-10-01',3}, having the number 3 because eventhough location is common but date is different.

Please find my code below.

var item = this.state.item;
var lines = item.order_items;
var count=1;
var key1;
var key2;

    for(var i=0;i<lines.length;i++)
        {
            key1 = lines[i].location + '-' + lines[i].date;

            for(var j=0;j<lines.length;j++)
                {
                    key2 = lines[j].location + '-' + lines[j].date;
                    if( key1 === key2 )
                        {
                            lines[i].number=count;
                            this.setState({item:item});
                        }
                    else
                        {
                            count++;
                            lines[j].number=count;
                            this.setState({item:item});
                        }
                }
        }

Problem is, for loop is iterating multiple times - its keep on comparing the same element multiple times. how do i can solve this issue.

Upvotes: 4

Views: 2003

Answers (3)

user1636522
user1636522

Reputation:

Using a map you could get a more readable code and probably a more efficient program, O(n²) to O(n) assuming that accessing a map is in constant time :

var n = 0;
var map = {};
var xs = [
  ['LA', '2017-12-01'],
  ['LA', '2017-12-01'],
  ['NY', '2017-12-01'],
  ['NY', '2016-10-01'],
  ['LA', '2017-12-01'],
  ['LA', '2017-12-01'],
  ['LA', '2017-12-01']
];

xs = xs.map(function (x) {
  var label = x[0] + "-" + x[1];
  if (!map[label]) map[label] = ++n;
  return x.concat([map[label]]);
});

console.log(xs);

Upvotes: 1

Raghav M
Raghav M

Reputation: 153

This seems like the sort of problem well suited for a map. You shouldn't be looping over the array twice.

var item = this.state.item;
var lines = item.order_items;
var map = new Map();
for (var i = 0; i < lines.length; i++) {
    key = lines[i].location + '-' + lines[i].date;
    if (!map.has(key)) {
        map.set(key, map.size + 1);
    }
    lines[i].number = map.get(key);
    this.setState({item: item});
}

Upvotes: 3

Jared Burgett
Jared Burgett

Reputation: 161

If you are trying to compare each item to one another, your innermost for loop is starting at the wrong index. Currently you have

for(var j=0;j<lines.length;j++)

Instead, you should begin where your outer loop left off, like so (by incrementing i by one)

for(var j=i+1;j<lines.length;j++)

Hope this helps!

Upvotes: 1

Related Questions