Remove duplicate array based on api response attribute value

Remove duplicate array based on api response attribute value is something different than a normal array. In this post we will find out how we can achieve by comparing attribute value of object array and remove the duplicate array.

Remove duplicate array simple

In the below code what we are doing is, we loop through the original array and check with the ‘if’ condition se see if our blank new array doesn’t have any of original array. If that returns true then we are going to push original array to new array which is going to add only new numbers.

Code


var originalArray = [2,6,6,9,9,8,8,8,7,4,2];
var newArray = [];
for (i=0; i<originalArray.length; i++) {  
    if (!newArray.includes(originalArray[i])) {
        newArray.push(originalArray[i]); 
    }
}
console.log(newArray); // [2,6,9,8,7,4];

Remove duplicate array based on api response attribute value

Now what if you want to check that if the attribute value is duplicate. In the below response from API you can see that the attribute_value has some duplicate number comparing to other arrays.

Code


[
  {
    "id": "484822",
    "activity_attributes": [
      {
        "id": "868117",
        "activity_id": "484822",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484884",
    "activity_attributes": [
      {
        "id": "868175",
        "activity_id": "484884",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484888",
    "activity_attributes": [
      {
        "id": "868182",
        "activity_id": "484888",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484823",
    "activity_attributes": [
      {
        "id": "868120",
        "activity_id": "484823",
        "attribute_name": "position",
        "attribute_value": "2",
      }
    ]
  },
  {
    "id": "484975",
    "activity_attributes": [
      {
        "id": "868344",
        "attribute_name": "position",
        "attribute_value": "2",
      }
    ]
  },
  {
    "id": "484891",
    "activity_attributes": [
      {
        "id": "868189",
        "attribute_name": "position",
        "attribute_value": "3",
      }
    ]
  },
  {
    "id": "484903",
    "activity_attributes": [
      {
        "id": "868200",
        "attribute_name": "position",
        "attribute_value": "4",
      },
    ]
  }
]

In order to remove duplicate array in such condition, we will perform to loop through an array. Then we will store its identifier by some function and then only return the first instance of each item.

Code

const filterByIteratee = (array, iteratee) => {

  // Empty object to store attributes as we encounter them
  const previousAttributeNames = {

  }

  return array.filter(item => {
    // Get the right value
    const itemValue = iteratee(item)

    // Check if we have already stored this item
    if (previousAttributeNames.hasOwnProperty(itemValue)) return false
    else {
      // Store the item so next time we encounter it we filter it out
      previousAttributeNames[itemValue] = true
      return true  
    }
  })
}

Now use the above function like below
code

const uniqueLinks = filterByIteratee(social_post_link, item =>
  item.activity_attributes[0].attribute_value
)

Now the above code will return my desired output.

Code


    [
  {
    "id": "484822",
    "activity_attributes": [
      {
        "id": "868117",
        "activity_id": "484822",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484823",
    "activity_attributes": [
      {
        "id": "868120",
        "activity_id": "484823",
        "attribute_name": "position",
        "attribute_value": "2",
      }
    ]
  },
  {
    "id": "484891",
    "activity_attributes": [
      {
        "id": "868189",
        "attribute_name": "position",
        "attribute_value": "3",
      }
    ]
  },
  {
    "id": "484903",
    "activity_attributes": [
      {
        "id": "868200",
        "attribute_name": "position",
        "attribute_value": "4",
      },
    ]
  }
]

Note: This might not be the best solution but it is very easy to understand. Reference link

Happy Learning.

HTML5 default validation for confirm password

Hello guys,
In this section, I am going to show you how you can easily validating HTML5 default validation for confirm the password. Generally in HTML5 when you create a form you simply write “required” attribute to the form element and it starts validating itself without other javascript code. But for confirm password validation you would required to add few lines of code that will set the validation in the default way.

JavaScript Code


var password = document.getElementById("password")
  , confirm_password = document.getElementById("confirm_password");

function validatePassword(){
  if(password.value != confirm_password.value) {
    confirm_password.setCustomValidity("Passwords Don't Match");
  } else {
    confirm_password.setCustomValidity('');
  }
}

password.onchange = validatePassword;
confirm_password.onkeyup = validatePassword;

HTML5 form


Confirm password Validation

How to bypass administrator password in windows

Hello all,

In this article I am going to show you How to bypass administrator password windows 7, 8, 10.

Bypass administrator password windows

  1. Open Run dialog box by pressing Window key + R
  2. Type netplwiz and press enter.
  3. Uncheck the option under user tab that says Users must enter a user name and password to use this computer
  4. Click on Apply and it will ask for a Current password for verification.
    bypass administrator password windows netplwiz
  5. Click on ok to close password dialog box and click on ok again to close User account dialog box.
  6. Now Restart your computer and see the magic.

Credit url: Techworm

How to view wifi password from command prompt

In this simple article I am going to show you how to view wifi password from command prompt. This is very simple trick by using command prompt that allows you to see the password of connected network till now.

Note: It only work on the network you have been connected so far till now. It doesn’t show the password of new profile which is not connected yet.

See how to view wifi password from command prompt

To view the password first we need to get the list of profiles that have been connected to our laptop. Complete the simple steps and get to know how to view wifi password from command prompt

  • Open command prompt as administrator. Right button click on command prompt icon and click Run as Administrator.
  • Type netsh wlan show profile
    (It will show all the list of profile that have been connected in your system.)
  • Now in order to view the password of any profile listed under, just type
  • netsh wlan show profile wifi-name key=clear
    (where wifi-name is the name of one of the profile listed.)

Nested Loop javascript

Simple nested loop javascript code



var person = [{
    name : [
	   {firstName: "Ravi", lastName: "Kumar"}
	],
    id       :  5566
},{
	name: [
		{firstName: "John", lastName: "Braman"}
	],
    id       :  96
}];

var item, text;
text="<table class='stack'>";
text+="<thead><tr><th>First Name</th><th>Last Name</th><th>id</th></tr>";
 for(var i=0; i<person.length; i++) {
	for(var j=0; j<person[i].name.length; j++) {
	    text += "<tr><td>"+person[i].name[j].firstName+"</td><td>"+person[i].name[j].lastName+"</td>"+"<td>"+person[i].id+"</td></tr>";
	}
}
text+= "</table>";
document.getElementById("demo").innerHTML = text;