Get random X items from array in javascript - javascript

I have the following fiddle using test data, and I am looking through data using $.each
I can loop through no problem, but I want to loop through the data, and then get 3 objects from it at random.
Any tips or tricks would help:
https://jsfiddle.net/inkedraskal/pah44qv6/
$.each(testData,function(x, blah){
//console.log(blah._id);
//this gets each objects id & picture, but I want to get 3 random objects, and their corresponding data
var activeValue = blah._id,
pictureValue = blah.picture;
var markUp = '';
markUp += activeValue + pictureValue;
console.log(markUp);
});
with the question below, they need to be unique**

The function in the snippet below gets an array and a number (X) of items and returns a new array with X unique random items from the original array:
function getRandomItems(arr, items) {
var ret = [];
var indexes = [];
var arr_length = arr.length;
// If we don't have enough items to return - return the original array
if (arr_length < items) {
return arr;
}
while (ret.length < items) {
i = Math.floor(Math.random() * arr_length);
if (indexes.indexOf(i) == -1) {
indexes[indexes.length] = i;
ret[ret.length] = arr[i];
}
}
return ret;
}
arr = ['a', 'b', 'c', 'd', 'e']
console.log(getRandomItems(arr, 2))
You can also add the function to the Array.prototype if you want to use it on every Array in your code as a "native" function:
Array.prototype.getRandomItems = function(items) {
var ret = [];
var indexes = [];
var arr_length = this.length;
// If we don't have enough items to return - return the original array
if (arr_length < items) {
return this;
}
while (ret.length < items) {
i = Math.floor(Math.random() * arr_length);
if (indexes.indexOf(i) == -1) {
indexes[indexes.length] = i;
ret[ret.length] = this[i];
}
}
return ret;
}
arr1 = ['a', 'b', 'c', 'd', 'e']
arr2 = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
console.log(arr1.getRandomItems(1))
console.log(arr1.getRandomItems(2))
console.log(arr2.getRandomItems(3))
console.log(arr2.getRandomItems(4))

We can create getRandomEntry() function which will return one of the array's elements by using Math.random() function. We'll create a loop which will take random entry every time it iterates.
function getRandomEntry() {
return testData[Math.round(Math.random() * (testData.length - 1))];
}
for (var i=0; i<3; i++) {
var entry = getRandomEntry();
console.log(entry._id, entry.picture);
}
If you need an unique entry every time. You can keep random entries in separate array and check if new one is unqiue.
var randomEntries = [];
function getRandomEntry() {
return testData[Math.round(Math.random() * (testData.length - 1))];
}
function entryExists(entry) {
return randomEntries.indexOf(entry) > -1;
}
for (var i=0; i<3; i++) {
var entry;
do {
entry = getRandomEntry();
} while(entryExists(entry))
randomEntries.push(entry);
console.log(entry._id, entry.picture);
}

You could use a recursive function like the below.
Note that this implementation prevents duplicates in the results
function getRandomObjects(array,selected,needed){
/*
*#param array array The array to pull from
*#param selected array The array of results pulled so far
*#param needed int The number of results we want
*/
var length = array.length;
var num = Math.floor(Math.random() * length) + 1; // get random number in bounds of array
var exists=false; // make sure we didnt already pick this object
$.each(selected,function(i,obj){
if(obj.index==num)exists=true;
})
if(exists) getRandomObjects(array,selected,needed); // get a new one if this was a duplicate
else selected.push(array[num]);
if(selected.length!=needed) return getRandomObjects(array,selected,needed); // get another object if we need more
else return selected; // return the final result set
}
var testData = [
{
"_id": "57e5d1a90c4206b128cd8654",
"index": 0,
"guid": "1f3269fc-0822-4c5a-9c52-8055155b407e",
"isActive": true,
"balance": "$3,026.95",
"picture": "http://placehold.it/32x32"
},
{
"_id": "57e5d1a9a986ccb2f41cf7b9",
"index": 1,
"guid": "a6b726b6-6466-4e48-8697-1c6bd7b1c79e",
"isActive": true,
"balance": "$2,642.74",
"picture": "http://placehold.it/32x32"
},
{
"_id": "57e5d1a9f98f8b2f6880de32",
"index": 2,
"guid": "e7d736cc-19e0-4bcb-8d0a-4d17442d8cee",
"isActive": true,
"balance": "$3,341.64",
"picture": "http://placehold.it/32x32"
},
{
"_id": "57e5d1a9e40ded5b017e45cd",
"index": 3,
"guid": "64230ca8-05c0-4c39-a931-794172475a32",
"isActive": true,
"balance": "$2,196.13",
"picture": "http://placehold.it/32x32"
},
{
"_id": "57e5d1a90cc30be769a06d7c",
"index": 4,
"guid": "d6618b78-753a-4ad0-bc14-3687d0b99196",
"isActive": true,
"balance": "$1,611.62",
"picture": "http://placehold.it/32x32"
},
{
"_id": "57e5d1a92481a43f50607415",
"index": 5,
"guid": "35ec8186-9494-4f89-ab89-bed7f39872c3",
"isActive": true,
"balance": "$3,148.87",
"picture": "http://placehold.it/32x32"
},
{
"_id": "57e5d1a9164f17c558ba7ce1",
"index": 6,
"guid": "244970a0-1ce2-405a-8d69-c7903f9bf5eb",
"isActive": false,
"balance": "$3,758.13",
"picture": "http://placehold.it/32x32"
},
{
"_id": "57e5d1a95afde31c5cf592a8",
"index": 7,
"guid": "aa30c82d-dd2b-420c-8b30-7d66cec8d10b",
"isActive": true,
"balance": "$1,311.40",
"picture": "http://placehold.it/32x32"
}
]
var randomObjects=getRandomObjects(testData,[],3);
console.log(randomObjects);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

You can do this
var items = [1, 2, 3, 4, 5];
var newItems = [];
for(var i = 0; i < 3; i++) {
var idx = Math.floor(Math.random() * items.length);
newItems.push(items[idx]);
items.splice(idx, 1);
}
console.log(newItems);

Related

Repeat every element in array based on object properties

I have an array that I'm retrieving from an API. The array looks like this:
[{
"name": "Rachel",
"count": 4,
"fon": "46-104104",
"id": 2
},
{
"name": "Lindsay",
"count": 2,
"fon": "43-053201",
"id": 3
},
{
"name": "Michael",
"count": 5,
"fon": "46-231223",
"id": 4
}]
Then I loop through the array to create an array containing only the names.
function buildName(data) {
for (var i = 0; i < data.length; i++) {
nameList.push(data[i].name)
}
}
This also works so far, but I would like to create an array in which each name occurs as often as the object count says.
For example, the name Michael should appear five times in the array and Lindsay twice.
[
"Rachel",
"Rachel",
"Rachel",
"Rachel",
"Lindsay",
"Lindsay",
"Michael",
"Michael",
"Michael",
"Michael"
"Michael"
]
For each object create a new array using count, and then fill it with the name.
If you use flatMap to iterate over the array of objects. It will return a new array of nested objects but then flatten them into a non-nested structure.
const data=[{name:"Rachel",count:4,fon:"46-104104",id:2},{name:"Lindsay",count:2,fon:"43-053201",id:3},{name:"Michael",count:5,fon:"46-231223",id:4}];
const out = data.flatMap(obj => {
return new Array(obj.count).fill(obj.name)
});
console.log(out);
I've upgraded your functions but you can use the map method
function buildName(data){
for (let i = 0; i < data.length; i++){
let numToLoop = data[i].count
let name = data[i].name
for (let z = 0; z < +numToLoop; z++){
nameList.push(name)
}
}
}
Use an inner while loop inside the for loop:
const data = [{
"name": "Rachel",
"count": 4,
"fon": "46-104104",
"id": 2
},
{
"name": "Lindsay",
"count": 2,
"fon": "43-053201",
"id": 3
},
{
"name": "Michael",
"count": 5,
"fon": "46-231223",
"id": 4
}]
function buildName(data){
const result = [];
for (let i = 0; i < data.length; i += 1) {
let item = data[i];
let count = item.count;
while (count > 0) {
result.push(item.name);
count -= 1;
}
}
return result;
}
console.log(buildName(data));
Just add an inner loop with as many iterations as the "count" property in the object:
function buildName(data) {
const nameList = [];
for (var i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].count; j++) {
nameList.push(data[i].name);
}
}
return nameList;
}
For fun
import { pipe } from 'fp-ts/lib/function';
import { chain, replicate } from 'fp-ts/lib/Array';
const arr = ...
const result = pipe(
arr,
chain(i => replicate(i.count, i.name))
);
You can use .flapMap() for that:
const arr = [{ "name": "Rachel", "count": 4, "fon": "46-104104", "id": 2 }, { "name": "Lindsay", "count": 2, "fon": "43-053201", "id": 3 }, { "name": "Michael", "count": 5, "fon": "46-231223", "id": 4 }];
const result = arr.flatMap(({count, name}) => Array(count).fill(name));
console.log(result);
Effectively you turn every element into an array of the the name property repeated count times which is then flattened into a single array.
It can be done via creating an array with repeated names in this way:
Array(count).fill(name)
Then you have to spread it into resulting array.
You can try this one-liner
const getNames = (data) =>
data.reduce(
(names, { name, count }) => [...names, ...Array(count).fill(name)],
[]
)
Note that a pure function is presented here, which is generally the preferred way of writing code. However, updating your example code might look like this
const getNames = (data) =>
data.reduce(
(names, { name, count }) => [...names, ...Array(count).fill(name)],
[]
)
function buildName(data) {
nameList = getNames(data)
}

How to get the unique object by comparing two sets of arrays using Javascript

I have the following two arrays:
var data1=[
{
"id": 1,
"url": "http://192.168.1.165:90/asset/"
},
{
"id": 2,
"url": "Assigned"
}
]
var data2=[
{
"id": 1,
"url": "http://192.168.1.165:90/asset/"
},
{
"id": 2,
"url": "Assigned"
},
{
"id": 3,
"url": "Assigned"
}
]
Result:
var unique=[{ {
"id": 3,
"url": "Assigned"
}}]
How can I get the unique object from these two arrays ?
I have tried using a for loop like this :
var unique = [];
for(var i = 0; i < data2.length; i++){
var found = false;
for(var j = 0; data1.length; j++){
if(data2[i].id == data1[j].id){
found = true;
break;
}
}
if(found == false){
unique.push(array1[i]);
}
}
But wanted to get a solution using functional javascript...
Try like this
var joined = data1.concat(data2);
var temp = [];
joined.forEach(function (x) {
var objList=joined.filter(function(y){ return y.id == x.id});
if(objList.length == 1) // if data count of current item in merged array is 1 that's means it belong to only one data source
temp.push(x);
})
console.log(temp)
JSFIDDLE
try this: first get the objects from data1 which are not in data2 and remove from data2 if it is there then concat it with data2.
<script>
var data1=[
{
"id": 1,
"url": "http://192.168.1.165:90/asset/"
},
{
"id": 2,
"url": "Assigned"
}
];
var data2=[
{
"id": 1,
"url": "http://192.168.1.165:90/asset/"
},
{
"id": 2,
"url": "Assigned"
},
{
"id": 3,
"url": "Assigned"
}
];
var arr3 = [];
for(var i in data1){
var dup = false;
for (var j in data2){
if (data2[j].id == data1[i].id && data2[j].url == data1[i].url) {
data2.splice(j,1);
}
}
if(dup) arr3.push(arr1[i])
}
arr3 = arr3.concat(data2);
console.log(arr3);
</script>
Edited for resulting the single unique object!
Assuming you have a function:
function unique(arr) {
var uni = [];
for(var i=0; i<arr.length; ++i) {
var rep = -1;
for(var j=0; j<arr.length; ++j)
if(arr[i].id == arr[j].id) rep++;
if (!rep) uni.push(arr[i]);
}
return uni;
}
this would work and give you the single unique object:
var u = unique(data1.concat(data2));
The idea is to make an union of the two given arrays and then iterate through setA and look for the matching properties and values in setA and in union. If found, then the index is stored. If there are more than one index, delete all items from union whith the indices.
The rest is then the symmetric difference.
var data1 = [{ "id": 1, "url": "http://192.168.1.165:90/asset/" }, { "id": 2, "url": "Assigned" }],
data2 = [{ "id": 1, "url": "http://192.168.1.165:90/asset/" }, { "id": 2, "url": "Assigned" }, { "id": 3, "url": "Assigned" }];
function symmetricDifference(setA, setB) {
var union = setA.concat(setB);
setA.forEach(function (a) {
var aK = Object.keys(a),
indices = [];
union.forEach(function (u, i) {
var uK = Object.keys(u);
aK.length === uK.length &&
aK.every(function (k) { return a[k] === u[k]; }) &&
indices.push(i);
});
if (indices.length > 1) {
while (indices.length) {
union.splice(indices.pop(), 1);
}
}
});
return union;
}
document.write('<pre>' + JSON.stringify(symmetricDifference(data1, data2), 0, 4) + '</pre>');

JavaScript: Remove duplicates of objects sharing same property value

I have an array of objects that I would like to trim down based on a specific key:value pair. I want to create an array that includes only one object per this specific key:value pair. It doesn't necessarily matter which object of the duplicates is copied to the new array.
For example, I want to trim based on the price property of arrayWithDuplicates, creating a new array that only includes one of each value:
var arrayWithDuplicates = [
{"color":"red",
"size": "small",
"custom": {
"inStock": true,
"price": 10
}
},
{"color":"green",
"size": "small",
"custom": {
"inStock": true,
"price": 30
}
},
{"color":"blue",
"size": "medium",
"custom": {
"inStock": true,
"price": 30
}
},
{"color":"red",
"size": "large",
"custom": {
"inStock": true,
"price": 20
}
}
];
Would become:
var trimmedArray = [
{"color":"red",
"size": "small",
"custom": {
"inStock": true,
"price": 10
}
},
{"color":"green",
"size": "small",
"custom": {
"inStock": true,
"price": 30
}
},
{"color":"red",
"size": "large",
"custom": {
"inStock": true,
"price": 20
}
}
];
Is there a JavaScript or Angular function that would loop through and do this?
EDIT: The property to filter on is nested within another property.
This function removes duplicate values from an array by returning a new one.
function removeDuplicatesBy(keyFn, array) {
var mySet = new Set();
return array.filter(function(x) {
var key = keyFn(x), isNew = !mySet.has(key);
if (isNew) mySet.add(key);
return isNew;
});
}
var values = [{color: "red"}, {color: "blue"}, {color: "red", number: 2}];
var withoutDuplicates = removeDuplicatesBy(x => x.color, values);
console.log(withoutDuplicates); // [{"color": "red"}, {"color": "blue"}]
So you could use it like
var arr = removeDuplicatesBy(x => x.custom.price, yourArrayWithDuplicates);
I don't think there's a built-in function in Angular, but it isn't hard to create one:
function removeDuplicates(originalArray, objKey) {
var trimmedArray = [];
var values = [];
var value;
for(var i = 0; i < originalArray.length; i++) {
value = originalArray[i][objKey];
if(values.indexOf(value) === -1) {
trimmedArray.push(originalArray[i]);
values.push(value);
}
}
return trimmedArray;
}
Usage:
removeDuplicates(arrayWithDuplicates, 'size');
Returns:
[
{
"color": "red",
"size": "small"
},
{
"color": "blue",
"size": "medium"
},
{
"color": "red",
"size": "large"
}
]
And
removeDuplicates(arrayWithDuplicates, 'color');
Returns:
[
{
"color": "red",
"size": "small"
},
{
"color": "green",
"size": "small"
},
{
"color": "blue",
"size": "medium"
}
]
Use Array.filter(), keeping track of values by using an Object as a hash, and filtering out any items whose value is already contained in the hash.
function trim(arr, key) {
var values = {};
return arr.filter(function(item){
var val = item[key];
var exists = values[val];
values[val] = true;
return !exists;
});
}
You can use underscore for this:
//by size:
var uSize = _.uniqBy(arrayWithDuplicates, function(p){ return p.size; });
//by custom.price;
var uPrice = _.uniqBy(arrayWithDuplicates, function(p){ return p.custom.price; });
You can use lodash to remove duplicate objects:
import * as _ from 'lodash';
_.uniqBy(data, 'id');
Here 'id' is your unique identifier
Try the following function:
function trim(items){
const ids = [];
return items.filter(item => ids.includes(item.id) ? false : ids.push(item.id));
}
using lodash you can filter it out easily
the first parameter will be your array and second will be your field with duplicates
_.uniqBy(arrayWithDuplicates, 'color')
it will return an array with unique value
Simple solution although not the most performant:
var unique = [];
duplicates.forEach(function(d) {
var found = false;
unique.forEach(function(u) {
if(u.key == d.key) {
found = true;
}
});
if(!found) {
unique.push(d);
}
});
for (let i = 0; i < arrayWithDuplicates.length; i++) {
for (let j = i + 1; j < arrayWithDuplicates.length; j++) {
if (arrayWithDuplicates[i].name === students[j].name) {
arrayWithDuplicates.splice(i, 1);
}
}
}
this will work perfectly...and this will delete first repeated array.
To delete last repeated array we only have to change
arrayWithDuplicates.splice(i, 1) ; into
arrayWithDuplicates.splice(j, 1);
Off the top of my head there is no one function that will do this for you as you are dealing with an array of objects and also there is no rule for which duplicate would be removed as duplicate.
In your example you remove the one with size: small but if you were to implement this using a loop you'd most likely include the first and exclude the last as you loop through your array.
It may very well be worth taking a look at a library such as lodash and creating a function that uses a combination of it's API methods to get the desired behaviour you want.
Here is a possible solution you could use making use of basic Arrays and a filter expression to check whether a new item would be considered a duplicate before being attached to a return result.
var arrayWithDuplicates = [
{"color":"red", "size": "small"},
{"color":"green", "size": "small"},
{"color":"blue", "size": "medium"},
{"color":"red", "size": "large"}
];
var reduce = function(arr, prop) {
var result = [],
filterVal,
filters,
filterByVal = function(n) {
if (n[prop] === filterVal) return true;
};
for (var i = 0; i < arr.length; i++) {
filterVal = arr[i][prop];
filters = result.filter(filterByVal);
if (filters.length === 0) result.push(arr[i]);
}
return result;
};
console.info(reduce(arrayWithDuplicates, 'color'));
You can check out some literature on Array filtering here
If you need to provide a preference on which item to remove you could define extra parameters and logic that will make extra property checks before adding to a return value.
Hope that helps!
Here is the typescript way
public removeDuplicates(originalArray:any[], prop) {
let newArray = [];
let lookupObject = {};
originalArray.forEach((item, index) => {
lookupObject[originalArray[index][prop]] = originalArray[index];
});
Object.keys(lookupObject).forEach(element => {
newArray.push(lookupObject[element]);
});
return newArray;
}
And
let output = this.removeDuplicates(yourArray,'color');
This is just another 'feature' based on yvesmancera's solution (after I started tinkering for my own solution) Also noted we are only allowed to currently use IE 11, so limited ES5 is allowed.
var newArray = RemoveDuplicates(myArray,'Role', 2);
function RemoveDuplicates(array, objKey, rtnType) {
var list = [], values = [], value;
for (var i = 0; i < array.length; i++) {
value = array[i][objKey];
if(values.indexOf(value) === -1){
list.push(array[i]);
values.push(value);
}
}
if(rtnType == 1)
return list;
return values;
};
Hoping this will work for most, if not all arrays when filtering out objects based on a single object property value.

JavaScript compare two arrays(key/value pairs) and copy value from one to the other if key matches

I have two arrays containing key/value pairs.
{
"containerOne": [{
"Id": 1,
"Title": "TitleOne",
"Responsibility": "ValueOne"
}, {
"Id": 2,
"Title": "TitleTwo",
"Responsibility": "ValueTwo"
}]
}
{
"containerTwo": [{
"Id": 1,
"Title": "TitleOne",
"Responsibility": null
}, {
"Id": 2,
"Title": "TitleTwo",
"Responsibility": "null
}
]
}
I'd like to compare both arrays and compare the title of each container. If the titles match, then I'd like to copy the Responsibility value from containerOne to containerTwo. The ID's will not match, so that can be ruled out. Only the titles will be consistent.
What is the most efficient way to do this please?]
Thanks
=====================================================================
EDIT
=====================================================================
Looking at the arrays a little closer, there is a subtle difference in the data being returned:
{
"AMLookupTasksList":
[
{
"Id":1,
"Title":"Create and Maintain an Onboarding Document",
"Responsibility":"1. Onboarding|f101ccf1-c7d5-42e7-ba8f-48e88ac90a3d"
},
{
"Id":2,
"Title":"Execute Onboarding for New Consultants",
"Responsibility":"1. Onboarding|f101ccf1-c7d5-42e7-ba8f-48e88ac90a3d"
}
]
}
{
"AMTasksList":
[
{
"Id":4,
"Title":
{
"$M_1":13,"$c_1":"Create and Maintain an Onboarding Document"
},
"Responsibility":null
},
{
"Id":17,
"Title":
{
"$M_1":12,"$c_1":"Execute Onboarding for New Consultants"
},
"Responsibility":null
}
]
}
Do I have additional looping to get to the Title value in the second array?
This might be a bit of overkill but it ignores the sequence and does a look up in each object.
I had to fix some syntax in your objects but I include that: named the objects and took a quote off one of the null values.
var obj1 = {
"containerOne": [{
"Id": 1,
"Title": "TitleOne",
"Responsibility": "ValueOne"
}, {
"Id": 2,
"Title": "TitleTwo",
"Responsibility": "ValueTwo"
}]
};
var obj2 = {
"containerTwo": [{
"Id": 1,
"Title": "TitleOne",
"Responsibility": null
}, {
"Id": 2,
"Title": "TitleTwo",
"Responsibility": null
}]
};
Now the code:
// lookup for first object:
var lookup = {};
// create referece to list above and use it everywhere
lookup.list = obj1;
for (var i = 0, len = lookup.list.containerOne.length; i < len; i++) {
lookup[lookup.list.containerOne[i].Title] = lookup.list.containerOne[i];
}
// lookup for second object
var otherLookup = {};
otherLookup.list = obj2;
for (var i = 0, len = otherLookup.list.containerTwo.length; i < len; i++) {
otherLookup[otherLookup.list.containerTwo[i].Title] = otherLookup.list.containerTwo[i];
}
// copy value for Responsibility from first to second on each matched in second.
var i = 0;
var len = lookup.list.containerOne.length;
for (i; i < len; i++) {
// looks up value from second list in the first one and if found, copies
if (lookup[otherLookup.list.containerTwo[i].Title]) {
otherLookup.list.containerTwo[i].Responsibility = lookup[otherLookup.list.containerTwo[i].Title].Responsibility;
}
}
// alerts new value using lookup
alert(otherLookup["TitleOne"].Responsibility);
EDIT for new structure, but same answer really:
var obj1 = {
"AMLookupTasksList": [{
"Id": 1,
"Title": "Create and Maintain an Onboarding Document",
"Responsibility": "1. Onboarding|f101ccf1-c7d5-42e7-ba8f-48e88ac90a3d"
}, {
"Id": 2,
"Title": "Execute Onboarding for New Consultants",
"Responsibility": "1. Onboarding|f101ccf1-c7d5-42e7-ba8f-48e88ac90a3d"
}]
};
var obj2 = {
"AMTasksList": [{
"Id": 4,
"Title": {
"$M_1": 13,
"$c_1": "Create and Maintain an Onboarding Document"
},
"Responsibility": null
}, {
"Id": 17,
"Title": {
"$M_1": 12,
"$c_1": "Execute Onboarding for New Consultants"
},
"Responsibility": null
}]
};
var lookup = {};
// create refernece to list above and use it everywhere
lookup.list = obj1;
for (var i = 0, len = lookup.list.AMLookupTasksList.length; i < len; i++) {
lookup[lookup.list.AMLookupTasksList[i].Title] = lookup.list.AMLookupTasksList[i];
}
var otherLookup = {};
otherLookup.list = obj2;
for (var i = 0, len = otherLookup.list.AMTasksList.length; i < len; i++) {
otherLookup[otherLookup.list.AMTasksList[i].Title.$c_1] = otherLookup.list.AMTasksList[i];
}
// copy value for Responsibility from first to second
var i = 0;
var len = otherLookup.list.AMTasksList.length;
for (i; i < len; i++) {
if (lookup[otherLookup.list.AMTasksList[i].Title.$c_1]) {
otherLookup.list.AMTasksList[i].Responsibility = lookup[otherLookup.list.AMTasksList[i].Title.$c_1].Responsibility;
}
}
alert(otherLookup["Create and Maintain an Onboarding Document"].Responsibility);
Fiddle for second answer: http://jsfiddle.net/n22V8/
First, create a dictionary from containerTwo:
var c2dict = {};
var c2i = containerTwo.innerContainer;
for (var i = 0; i < c2i.length; i++) {
c2dict[c2i[i].Title] = c2i[i];
}
Now use this to do the copying of propertyies when titles match:
var c1i = containerOne.innerContainer;
for (var i = 0; i < c1i.length; i++) {
if (c2dict[c1i[i].Title]) {
c2dict[c1i[i].Title].Property = c1i[i].Property;
}
}
You should compare properties and set them as the following:
containerOne.innerContainer.forEach(function (element,index) {
containerTwo.innerContainer.forEach(function (element2,index2) {
if (element.Title === element2.Title && element.Property != element2.Property) {
element2.Property = element.Property;
}
});
});

JSON to filtered Array in JavaScript

I have this JSON string:
[
{
"pk": "alpha",
"item": [{
"child": "val"
}]
},
{
"pk": "beta",
"attr": "val",
"attr2": [
"child1"
]
},
{
"pk": "alpha",
"anotherkey": {
"tag": "name"
}
}
]
And I need to produce a filtered array without repeated PK, in the example above the last entry: "pk": "alpha","anotherkey": { ... should be eliminated from the output array. All this using JavaScript. I tried with the object JSON.parse but it returns many key,value pairs that are hard to filter for example "key=2 value=[object Object]".
Any help is greatly appreciated.
var data = JSON.parse(jsonString);
var usedPKs = [];
var newData = [];
for (var i = 0; i < data.length; i++) {
if (usedPKs.indexOf(data[i].pk) == -1) {
usedPKs.push(data[i].pk);
newData.push(data[i]);
}
}
// newData will now contain your desired result
var contents = JSON.parse("your json string");
var cache = {},
results = [],
content, pk;
for(var i = 0, len = contents.length; i < len; i++){
content = contens[i];
pk = content.pk;
if( !cache.hasOwnPropery(pk) ){
results.push(content);
cache[pk] = true;
}
}
// restuls
<script type="text/javascript">
// Your sample data
var dataStore = [
{
"pk": "alpha",
"item": [{
"child": "val"
}]
},
{
"pk": "beta",
"attr": "val",
"attr2": [
"child1"
]
},
{
"pk": "alpha",
"anotherkey": {
"tag": "name"
}
}
];
// Helper to check if an array contains a value
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] == obj) {
return true;
}
}
return false;
}
// temp array, used to store the values for your needle (the value of pk)
var tmp = [];
// array storing the keys of your filtered objects.
var filteredKeys = [];
// traversing you data
for (var i=0; i < dataStore.length; i++) {
var item = dataStore[i];
// if there is an item with the same pk value, don't do anything and continue the loop
if (tmp.contains(item.pk) === true) {
continue;
}
// add items to both arrays
tmp.push(item.pk);
filteredKeys.push(i);
}
// results in keys 0 and 1
console.log(filteredKeys);
</script>

Categories