0

here in this code i have defined 2 arrays temp_Array and storage_Array. when i input data it gets written to temp. i want to compare each index of temp with each index of storage so that i get storage as a unique array. so it pushes the value from temp to storage if and only if it does not exists there.

<!doctype html>
<html>
<head>
<title>jQuery UI Dialog - Default functionality</title>
<style>
body {
font-family: "Trebuchet MS", "Helvetica", "Arial",  "Verdana", "sans-serif";
               font-size: 62.5%;
}
</style>
<link rel="stylesheet" href="jquery-ui.css">
<script src="jquery-1.11.1.js"></script>
<script src="jquery-ui.js"></script>
<script>

$(document).ready(function() { 
$('path') .dblclick(function(){
$(function() {
$( "#dialog" ).dialog();
});
});
$('.cat') .click(function(){
$(function(){
$('#dialog').dialog('close');
});
});
});
</script>
</head>
<body>
<svg width="80" height="32">
<path d="M10 15 l15 0 l2.5 -5 l5 10 l5 -10 l5 10 l5 -10 l5 10 l2.5 -5 l15 0" stroke="black" stroke-width="2px" stroke-linejoin="bevel" fill="none"></path>
</svg>
<div id="dialog" title="Basic dialog" style="display:none">
<form>
Component-ID: <input type="text" name="id1" id ='id1'><br>
Componentval: <input type="text" name="val1" id ='val1'><br>
<input class="cat" type="button" value="Submit" onclick="loop();">
<script>
var storage_Array =[];
var temp_Array =[];
function writedata(){

//console.log(document.getElementById("id1"));
temp_Array.push({'id':  document.getElementById("id1").value, 'val': document.getElementById("val1").value});
//console.log(storage_Array);
}
for(var i=0;i < 5; i++){
writedata();
}
console.log(temp_Array);
console.log(storage_Array);
}
</script>
</form>
</div>
</body>
  • You could take a look at [underscore's uniq function](http://underscorejs.org/#uniq), if you don't want to code it up. – Sascha Wolf Jul 08 '14 at 06:51

1 Answers1

0

To check if something already exists in an array, you can usually use the native javascript method indexOf() or a utility method like jQuery's inArray().

However, because your arrays contain objects, you need to do a little more work. As discussed in this question, javascript doesn't provide a generic way to determine if two objects are equal based on their contents.

There are a couple of solutions. You could write a function isEqual(objectA, objectB) which checks the values of id and val. Every time you insert a new object you need to loop over the array and use this function to ensure the object doesn't already exist.

Another way is to change your storage structure. It depends on what the data is, but sometimes it might be appropriate to group by id inside an object. The storage format would be something like:

{
  id_1: [value_1, value_2],
  id_2: [value_3]
}

Then you can conditionally add to the storage by the following:

newId = document.getElementById("id1").value;
newVal = document.getElementById("val1").value;
if(!(newId in storage)) {
    storage[newId] = [newVal];
} else if(-1 === $.inArray(newVal, storage[newId])) {
    storage[newId].push(newVal);
}
Community
  • 1
  • 1
David Lewis
  • 161
  • 6