Finding an Object's value from an Array - javascript

Is there a simpler (or more efficient) way of achieving the following:
var _dataObjects = [{id:0, data:"data0", nextID:1},
{id:1, data:"data1", nextID:2},
{id:2, data:"data2", nextID:3} .. etc.];
generateNextPieceOfData();
function generateNextPieceOfData(){
var len = _dataObjects.length;
for ( var i = 0; i < len; i ++ ) {
var nextDataID = _dataObjects[i].nextID;
var nextData;
for ( var j = 0; j < len; j ++ ) {
if( _dataObjects[j].id == nextDataID ){
nextData = _dataObjects[j].data;
break;
}
}
}
}
The above example is abstracted from the problem I'm having and I realise the ID numbers are sequential in this instance but in the real problem nextID numbers do not run sequentially.
Thanks in advance.

Use the right data structure for your problem. Since you want to find an object by ID, create a hash map with the IDs as keys and objects as values:
var object_map = {};
for(var i = 0, l = _dataObjects.length; i < l; i++) {
objects[_dataObjects[i].id] = _dataObjects[i];
}
Then getting the next object is simply:
var next_object = object_map[someObject.nextID];
You still have iterate until some terminal condition is met though. For example:
function generatePath(id_a, id_b) {
var obj = object_map[id_a];
var path = [obj];
while (obj && obj.id !== id_b) {
obj = object_map[obj.nextID];
path.push(obj);
}
return path;
}

If your code works sequentially only, then you can sort the items by id or whatever and your code should work right? Try this:
_dataObjects = _dataObjects.sort(function(a, b) {
return a.id > b.id;
});

Related

Remove data from an array comparing it to an other array

I am trying to compare the items in "item" array and the copyofOpList array to retrieve the data occurrences in copyofOpList
this is my try:
var _deleteUsedElement1 = function(item) {
for (var i = 0; i < item.length-1; i++){
for (var j = 0; j< $scope.copyofOpList.length-1; j++){
if (item[i].operationCode == $scope.copyofOpList[j].code) {
$scope.copyofOpList.splice(j, 1);
} } } };
$scope.compareArrays = function() {
...Get data from web Service
_deleteUsedElement1(item);
}
the copyofOpList array has 14 elements,and the item array has 2 array
but my code deletes only one occurrence (the first),so please how can I correct my code,to retrieve any occurances in the copyofOpList array comparing to the item array
thanks for help
I'd try to avoid looping inside a loop - that's neither a very elegant nor a very efficient way to get the result you want.
Here's something more elegant and most likely more efficient:
var item = [1,2], copyofOpList = [1,2,3,4,5,6,7];
var _deleteUsedElement1 = function(item, copyofOpList) {
return copyofOpList.filter(function(listItem) {
return item.indexOf(listItem) === -1;
});
};
copyofOpList = _deleteUsedElement1(item, copyofOpList);
console.log(copyofOpList);
//prints [3,4,5,6,7]
}
And since I just noticed that you're comparing object properties, here's a version that filters on matching object properties:
var item = [{opCode:1},{opCode:2}],
copyofOpList = [{opCode:1},{opCode:2},{opCode:3},{opCode:4},{opCode:5},{opCode:6},{opCode:7}];
var _deleteUsedElement1 = function(item, copyofOpList) {
var iOpCodes = item.map(function (i) {return i.opCode;});
return copyofOpList.filter(function(listItem) {
return iOpCodes.indexOf(listItem.opCode) === -1;
});
};
copyofOpList = _deleteUsedElement1(item, copyofOpList);
console.log(copyofOpList);
//prints [{opCode:3},{opCode:4},{opCode:5},{opCode:6},{opCode:7}]
Another benefit of doing it in this manner is that you avoid modifying your arrays while you're still operating on them, a positive effect that both JonSG and Furhan S. mentioned in their answers.
Splicing will change your array. Use a temporary buffer array for new values like this:
var _deleteUsedElement1 = function(item) {
var _temp = [];
for (var i = 0; i < $scope.copyofOpList.length-1; i++){
for (var j = 0; j< item.length-1; j++){
if ($scope.copyofOpList[i].code != item[j].operationCode) {
_temp.push($scope.copyofOpList[j]);
}
}
}
$scope.copyofOpList = _temp;
};

unexpected token break in ternary conditional

The function below is intended to return the values from a (potentially nested) object as an array - with the list parameter being any object. If I move my break statement to after the for loop, I don't get any errors, but of course then my function doesn't behave as needed. What's wrong with the way I'm using break?
function listToArray(list) {
var objectArray = [];
function objectPeeler() {
let peel = Object.getOwnPropertyNames(list);
for(var i = 0; i < peel.length; i++) {
list[peel[i]] && typeof list[peel[i]] != 'object' ?
objectArray.push(list[peel[i]]):
list[peel[i]] ?
(list = list[peel[i]], objectPeeler()) :
break;
}
return objectArray;
}
objectPeeler();
}
In case anyone else has this issue: ternary operators only work with value expressions, not statements (like break) and aren't meant to be used in these cases.
This works:
function listToArray(list) {
var objectArray = [];
function objectPeeler() {
let peel = Object.getOwnPropertyNames(list);
for(var i = 0; i < peel.length; i++) {
list[peel[i]] != null && typeof list[peel[i]] != 'object' ?
objectArray.push(list[peel[i]]):
list[peel[i]] ?
(list = list[peel[i]], objectPeeler()): null;
}
}
objectPeeler();
return objectArray;
}
But using the jquery .next method allows a better solution:
function listToArray(list) {
var array = [];
for (var obj = list; obj; obj = obj.next)
array.push(obj.value);
return array;
}
why not writing something like this :
var obj = { 0: "a", 1: "b", 2: "c"}; //test target
var objectArray = [];
var keyArray = Object.getOwnPropertyNames(obj);
for (var i = 0; i < keyArray.length; i++) objectArray.push(obj[keyArray[i]]);
console.log(objectArray); // test result

Javascript: attach child several deep to object?

If I want to iterate through a list like this:
var inputArray = [
'CHILD0',
'PARENT0_CHILD1',
'PARENT1_PARENT2_CHILD2',
'PARENT1_PARENT3_CHILD3',
'PARENT1_PARENT3_CHILD4'
];
And have it return an object like so:
var resultObject = {
CHILD0: null,
PARENT0: {CHILD1: null},
PARENT1: {
PARENT2: {CHILD2: null},
PARENT3: {
CHILD3: null,
CHILD4: null
}
}
};
How could I iterate through the array to return the result?
I've got something like this:
function iterateArray (inputArray) {
var _RESULT = {};
for (var i = 0; i < inputArray.length; i += 1) {
var _inputName = inputArray[i];
var _inputNameArray = _input.split('_');
var _ref;
for (var n = 0; n < _inputNameArray.length; n += 1) {
//...?
}
_RESULT[_ref] = null;
}
return _RESULT;
}
var resultObject = iterateArray(inputArray);
Not sure what to do from this point. Think I might need a recursive function of sorts. Thoughts?
With minimal changes to your code:
function iterateArray (inputArray) {
var _RESULT = {};
for (var i = 0; i < inputArray.length; i += 1) {
var _inputName = inputArray[i];
var _inputNameArray = _inputName.split('_');
var _ref = _RESULT;
for (var n = 0; n < _inputNameArray.length - 1; n += 1) {
if (!_ref[_inputNameArray[n]]) _ref[_inputNameArray[n]] = {};
_ref = _ref[_inputNameArray[n]];
}
_ref[_inputNameArray[n]] = null;
}
return _RESULT;
}
You never need recursion, as it can always be unwrapped into iteration (and vice versa). Things are just sometimes much nicer one way or another.
EDIT: What's with all the underscores? :)
EDIT2: The key point to understanding this is reference sharing. For example:
For CHILD0, _ref = _RESULT means both of the variables are pointing at the same {}. When you do _ref['CHILD0'] = null, it is the same as doing _RESULT['CHILD0'] = null.
For PARENT0_CHILD1, first _ref = _RESULT as above, so _ref['PARENT0'] = {} is the same as _RESULT['PARENT0'] = {}. Then we switch the meaning of _ref to be the same thing as _RESULT['PARENT0']; when we assign _ref['CHILD1'] = null, it is the same as assigning _RESULT['PARENT0']['CHILD1'] = null.

Remove items from one array if not in the second array

I state that I have tried for a long time before writing this post.
For an InDesign script, I'm working with two array of ListItems. Now I'm trying to remove the items of one array that aren't in the second array, but i'm stuck.
Given that I use the following javascript code (it works great) to remove the equal items between the two arrays :
function check_dupli(arr_A, arr_B) {
for(var i = arr_B.length - 1; i >= 0; i--) {
for(var j = 0; j < arr_A.length; j++) {
if(arr_B[i] === arr_A[j]) {
arr_B.splice(i, 1);
}
}
}
arr_B.sort();
}
arr_A = ["a","b","d","f","g"]
arr_B = ["a","c","f","h"]
check_dupli(arr_A, arr_B) --> arr_B = ["c","h"]
check_dupli(arr_B, arr_A) --> arr_B = ["b","d","g"]
I thought to modify it in order to ignore the items not that are not in both arrays, and to obtain what I want, but something is going wrong because I also get the unwanted data :
function get_dupli(arr_A, arr_B, arr_C) {
for(var e = arr_B.length - 1; e >= 0; e--) {
for(var k = 0; k < arr_A.length; k++) {
if(arr_B[e] === arr_A[k]) {
arr_C.push(arr_B[e]);
}
}
}
arr_C.sort();
}
arr_A = ["a","b","d","f","g"]
arr_B = ["a","g","k"]
arr_C = ["h"]
get_dupli(arr_A, arr_B, arr_C) --> arr_C = ["a","g","h","k"] instead of --> ["a","g","h"]
get_dupli(arr_B, arr_A, arr_C) --> arr_C = ["a","b","d","f","g","h"] instead of --> ["a","g","h"]
Where I'm wrong? There is another way in pure javascript to solve the problem?
Thanks in advance for any help.
You can make use of Array.prototype.filter and Array.prototype.concat to simply it:
arr_A = ["a","b","d","f","g"]
arr_B = ["a","g","k"]
arr_C = ["h"]
function getCommonItems(arrayA, arrayB, result) {
result = result || [];
result = result.concat(arrayA.filter(function(item) {
return arrayB.indexOf(item) >= 0;
}));
return result.sort();
}
alert(getCommonItems(arr_A, arr_B, arr_C).join(", "));
alert(getCommonItems(arr_B, arr_A, arr_C).join(", "));
For the first scenario:
arr_A = ["a","b","d","f","g"]
arr_B = ["a","c","f","h"]
function getDifference(arrayA, arrayB, result) {
return arrayB.filter(function(item) {
return arrayA.indexOf(item) === -1;
}).sort();
}
alert(getDifference(arr_A, arr_B).join(", "));
alert(getDifference(arr_B, arr_A).join(", "));
Do it like this:
//the array which will loose some items
var ar1 = ["a", "b", "c"];
//the array which is the template
var ar2 = ["d", "a", "b"];
var tmpar = [];
for(var i = 0; i < ar1.length; i++){
if(ar2.indexOf(ar1[i]) !== -1){
tmpar.push(ar1[i]);
}
}
ar1 = tmpar;
alert(ar1);
We create a temporary array to store the valid values.
We make sure that the index of the value from the first array is not "-1". If it's "-1" the index is not found and therefore the value is not valid! We store everything which is not "-1" (so we store every valid value).
Array.prototype.contains = function ( object )
{
var i = 0, n = this.length;
for ( i = 0 ; i < n ; i++ )
{
if ( this[i] === object )
{
return true;
}
}
return false;
}
Array.prototype.removeItem = function(value, global) {
var idx;
var n = this.length;
while ( n-- ) {
if ( value instanceof RegExp && value.test ( this[n])
|| this[n] === value ) {
this.splice (n, 1 );
if ( !global ) return this;
}
}
return this;
};
arr_A = ["a","b","d","f","g"];
arr_B = ["a","c","f","h"];
var item
while ( item = arr_A.pop() ) {
arr_B.contains ( item ) && arr_B.removeItem ( item );
}
arr_B;
arr_A = ["a","b","d","f","g"];
arr_B = ["a","c","f","h"];
var newArr = [];
var item
while ( item = arr_B.shift() ) {
arr_A.contains ( item ) && newArr[ newArr.length ] = item ;
}
newArr;// ["a", "f"];
Opsss .... I believed I had given the answer and closed this post ... sorry !!!
Despite all the checks I made, the failure of the mine as your script was caused by a stupid mistake ... the array arr_A passed to the function was a modified copy of the original array.
Thank you all for your concern and help. Sorry again ...

How to combine these two JavaScript arrays

I have two JavaScript arrays below that both have the same number of entries, but that number can vary.
[{"branchids":"5006"},{"branchids":"5007"},{"branchids":"5009"}]
[{"branchnames":"GrooveToyota"},{"branchnames":"GrooveSubaru"},{"branchnames":"GrooveFord"}]
I want to combine these two arrays so that I get
[{"5006":"GrooveToyota"},{"5007":"GrooveSubaru"},{"5008":"GrooveFord"}]
I'm not sure how to put it into words but hopefully someone understands. I would like to do this with two arrays of arbitrary length (both the same length though).
Any tips appreciated.
It's kind of a zip:
function zip(a, b) {
var len = Math.min(a.length, b.length),
zipped = [],
i, obj;
for (i = 0; i < len; i++) {
obj= {};
obj[a[i].branchids] = b[i].branchnames;
zipped.push(obj);
}
return zipped;
}
Example (uses console.log ie users)
var ids = [{"branchids":"5006"},{"branchids":"5007"},{"branchids":"5009"}];
var names = [{"branchnames":"GrooveToyota"},{"branchnames":"GrooveSubaru"},{"branchnames":"GrooveFord"}];
var combined = [];
for (var i = 0; i < ids.length; i++) {
var combinedObject = {};
combinedObject[ids[i].branchids] = names[i].branchnames;
combined.push(combinedObject);
}
combined; // [{"5006":"GrooveToyota"},{"5006":"GrooveSubaru"},{"5006":"GrooveFord"}]
Personally, I would do it IAbstractDownvoteFactor's way (+1), but for another option, I present the following for your coding pleasure:
var a = [{"branchids":"5006"},{"branchids":"5007"},{"branchids":"5009"}];
var b = [{"branchnames":"GrooveToyota"},{"branchnames":"GrooveSubaru"},{"branchnames":"GrooveFord"}];
var zipped = a.map(function(o,i){ var n={};n[o.branchids]=b[i].branchnames;return n;});
similar to #robert solution but using Array.prototype.map
var ids = [{"branchids":"5006"},{"branchids":"5007"},{"branchids":"5009"}],
names = [{"branchnames":"GrooveToyota"},{"branchnames":"GrooveSubaru"},{"branchnames":"GrooveFord"}],
merged = ids.map(function (o, i) { var obj = {}; obj[o.branchids]=names[i].branchnames; return obj; });
merged; //[{5006: "GrooveToyota"}, {5006: "GrooveSubaru"}, {5006:"GrooveFord"}]
Cheers!

Categories

Resources