I have a UI which has a HTML table where we can add dynamic rows and columns (angularjs)
The table has 2 static columns ID & Description, rest of the columns are dynamic columns where user has to add one or more columns.(button click) One the Save button click I want to validate if user has added unique dynamic columns or not.
Below Variable holds the array of the rows as below:
$scope.myTable.Rows
Below is the array generated when user adds just one dynamic column and 2 rows. Here id & description are the static column whereas "0" is the dynamic column added.
{0: "1111", description: "desc1", id: "1111"}
{0: "2222", description: "desc2", id: "2222"}
Another example when 3 dynamic columns, 3 rows are added:
{0: "a1", 1: "a2", 2: "a3", description: "desc1", id: "1111"}
{0: "a5", 1: "a6", 2: "a7", description: "desc2", id: "2222"}
{0: "a9", 1: "a10", 2: "a11", description: "desc3", id: "3333"}
I want to check if the above array is unique only with the combination of dynamic columns. ie as per the above array set values of dynamic columns 0,1,2 should not be repeated in any other row in the array.
So below array set should not pass validation:
{0: "a1", 1: "a2", 2: "a3", description: "desc1", id: "1111"}
{0: "a5", 1: "a6", 2: "a7", description: "desc2", id: "2222"}
{0: "a1", 1: "a2", 2: "a3", description: "desc3", id: "3333"}
As you see the 3rd row above is similar to 1st row. I dont want to take into account column "id" & "description"
While below is still unique:
{0: "a1", 1: "a2", 2: "a3", description: "desc1", id: "1111"}
{0: "a5", 1: "a6", 2: "a7", description: "desc2", id: "2222"}
{0: "a1", 1: "a2", 2: "a9", description: "desc3", id: "3333"}
I have tried looking at couple of posts here at: How can I check if the array of objects have duplicate property values?
But most of it account for looking at a single property and see if there is duplicate but not at a combined set.
I hope I have provided a clear explanation of my requirement. Let me know if you need more information.
--Updated--
I have tried with as:
let array = [
{0: "a1", 1: "a2", 2: "a3", description: "desc1", id: "1111"},
{0: "a5", 1: "a6", 2: "a7", description: "desc2", id: "2222"},
{0: "a1", 1: "a2", 2: "a3", description: "desc3", id: "3333"}];
let jsonRows = JSON.parse(JSON.stringify(array));
var valueArr = jsonRows.map(function(item){ return item."0" });
//above gives error: Uncaught SyntaxError: Unexpected string
var isDuplicate = valueArr.some(function(item, idx){
return valueArr.indexOf(item) != idx
});
console.log(jsonRows);