Remove Duplicate Object from JSON Array
uniqueArray.indexOf
doesn't work because you're comparing objects against strings (splitlen[i].name
). Try to use .find()
instead:
var arr1 = '[{"name":"Pune","city":"India"},{"name":"Pune","city":"India"}]';
var splitlen = JSON.parse(arr1);
var uniqueArray = [];
var uniqueJson = {};
for(i=0;i<splitlen.length;i++)
{
if(!uniqueArray.find(x => x.name === splitlen[i].name))
{
uniqueArray.push(splitlen[i]);
}
}
console.log(uniqueArray);
How to remove duplicate values from a json array using node js?
You can use array#map
and JSON.stringify
to create string from object and then use Set
to get all unique string and then convert back to object using array#map
and JSON.parse()
.
Following proposal won't work if the data type isn't JSON.
const temp = [{first: 569, second: "789", third: "458"}, {first: 476, second : "147", third: "369"}, {first: 100, second: "200", third: "300"}, {first: 100, second: "200", third: "300"}, {first: 100, second: "200", third: "300"}];
const result = [...new Set(temp.map(obj => JSON.stringify(obj)))] .map(str => JSON.parse(str));console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Remove duplicate objects from JSON file in JavaScript
You must load the JSON data in to the program and parse that with JSON.parse
, like this
var array = JSON.parse(content.toString())
To filter out the repeated names from the array of Objects, we use Array.prototype.filter
function. You can store the names in an object, and next time when the same name appears we simply filter it out from the result.
var seenNames = {};
array = array.filter(function(currentObject) {
if (currentObject.name in seenNames) {
return false;
} else {
seenNames[currentObject.name] = true;
return true;
}
});
console.log(array);
# [ { name: 'Peter', age: 30, 'hair color': 'brown' },
# { name: 'Steve', age: 55, 'hair color': 'blonde' } ]
Remove duplicate values from json array in PHP
Seems like others answers don't see their own final results and don't read desired output.
Here a little bit hard solution but it works well.
Note: the input data array must have 2 object indexes and 2 arrays of objects for comparing, otherwise, it should be fixed.
$ar = Array (
0 => [(object)["name"=>"John", "surname"=>"Smith"], (object)["name"=>"Kate", "surname"=>"Winston"]],
1 => [(object)["name"=>"Kate", "surname"=>"Winston"], (object)["name"=>"Jack", "surname"=>"Irving"]]
);
$arr = [];
$k = 0; // do `if statement` only once
foreach($ar as $num=>&$subar){
foreach($subar as $num2=>$pers){
$subar[$num2] = (array)$pers; // object to array
if (!$k) {
$keys = array_keys($subar[$num2]); // collect "name" and "surname" in an array
$k++;
}
foreach($subar[$num2] as $a=>$b){
$seq = array_search($a,$keys); // index of the current key
if (!$seq) { // 0 -> "name", 1 -> "surname"
$arr[$num][$b] = '';
} else {
$arr[$num][$subar[$num2][current($keys)]] = $b;
}
}
}
}
$diff[] = array_diff($arr[0],$arr[1]); // clear duplicates from 1-st set
$diff[] = array_diff($arr[1],$arr[0]); // clear duplicates from 2-nd set
Gives result:
Array
(
[0] => Array
(
[John] => Smith
)
[1] => Array
(
[Jack] => Irving
)
)
And after you can re-create the output array:
// creating a new array
$res = [];
foreach($diff as $num=>$ns){
foreach($ns as $name=>$surname){
foreach($keys as $ind=>$key){
if ($ind % 2 == 0){
$tmp[$key] = $name; // put name
} else {
$tmp[$key] = $surname; // put surname
}
}
$res[$num] = (object)$tmp; // array to object
}
}
Output will be:
Array
(
[0] => stdClass Object
(
[name] => John
[surname] => Smith
)
[1] => stdClass Object
(
[name] => Jack
[surname] => Irving
)
)
Demo
In case of string values in the input arrays, i.e.:
$ar = [
'[{"name":"John", "surname":"Smith"}, {"name":"Kate", "surname":"Winston"}]',
'[{"name":"Kate", "surname":"Winston"}, {"name":"Jack", "surname":"Irving"}]'
];
You need a little fix:
...
foreach($ar as $num=>&$subar){
$ar[$num] = json_decode($subar);
foreach($subar as $num2=>$pers){
...
The same output you will get.
Demo
Removing duplicates from JSON array by a value in each JSON object in array
This should do it:
names_array = [
{name: "a", age: 15},
{name: "a", age: 16},
{name: "a", age: 17},
{name: "b", age: 18},
{name: "b", age: 19}];
function hash(o){
return o.name;
}
var hashesFound = {};
names_array.forEach(function(o){
hashesFound[hash(o)] = o;
})
var results = Object.keys(hashesFound).map(function(k){
return hashesFound[k];
})
The hash
function decides which objects are duplicates, the hashesFound
object stores each hash value together with the latest object that produced that hash, and the results
array contains the matching objects.
Trying to remove duplicates from JSON array with array_unique and array_values
Use below solution
$array1 = '{"results":[{"cat_id":2,"cat_name":"veterinarian"},{"cat_id":3,"cat_name":"electrician"},{"cat_id":4,"cat_name":"dentist"}]}';
$array2 = '{"results":[{"cat_id":"8","cat_name":"dental hygienist"},{"cat_id":"5","cat_name":"stocktaker"},{"cat_id":"9","cat_name":"builder"}]}';
$array3 = '{"results":[{"cat_id":4,"cat_name":"dentist"},{"cat_id":5,"cat_name":"stocktaker"},{"cat_id":3,"cat_name":"electrician"}]}';
$array1 = json_decode($array1, TRUE);
$array2 = json_decode($array2, TRUE);
$array3 = json_decode($array3, TRUE);
$array4 = array_merge_recursive($array1['results'], $array2['results'], $array3['results']);
$uniqueArray['results'] = array_values(array_unique($array4, SORT_REGULAR));
Related Topics
Sticky Header Smoothing Scroll Down
How to Remove Time from Datetime in SQL
Label and Input Fields on the Same Line
Get Part of the Url Pathname Via JavaScript Regex
How to Completely Remove Ionic and Cordova Installation from Mac
How to Push Array to Json Object in Angular
How to Make Select Option Disable
How to Remove Captcha Verification from Firebase Phone Auth Using JavaScript
Expand Div on Click With Smooth Animation
How to Strip/Replace Something from a Url
Connecting to Tcp Socket from Browser Using JavaScript
How to Close Bootstrap 3 Dropdown When We Click Outside on a Tablet
Javascript, Viewing [Object Htmlinputelement]
Javascript - How to Remove All Extra Spacing Between Words
Prevent Form Redirect or Refresh on Submit
Why Does JavaScript Map Function Return Undefined
Datatables - Search in Multiple Columns With a Single Drop Down