Dynamically add key/value array of names into array of objects - javascript

I have an array of names. I also have an array of objects. I would like to iterate through the array of objects and also iterate through the the array of names and add the name into the objects. For example, name[0] goes into object[0], and so on.
I have this code:
this.individualSrv.GetDataById(this.org[i].userId).subscribe(data => {
this.names.push(data.fullname)
for (var x = 0; x < this.org.length; x++) {
for (var i in this.names) {
this.org[x]['name'] = this.names[i]
}
}
})
Right now, the last name in the array is added to each object in the array.

You don't need to nest 2 loops to do that. Just make sure that both arrays have the same length.
this.individualSrv.GetDataById(this.org[i].userId).subscribe(data => {
this.names.push(data.fullname)
for (var x = 0; x < this.org.length; x++) {
this.org[x]['name'] = this.names[x]
}
})

Related

Updating array based on newer objects in another array

I have a question that is hard to describe. It's a tale of two arrays.
I have one array holding 50 objects. Per object, I want to call its specific property "IDnumber".
The second array is one that has 5 variables containing all of the mentioned 50 objects's "IDnumber" property, with each variable having a set of 10 IDnumbers: var1 is the IDnumbers of the first 10 objects, var2 is is the IDnumbers of the second set, and so on until the 50th object's ID number.
This is where it gets hard.
If I want to add in a new object in the first array, so that there'll be 51 objects, then I want to update the second array with a 6th variable, that contains all the remaining objects' IDnumbers (in this case just the 1). I want array2's length to be dependent on array1's length.
var arr1 = [], obj = {"IDNumber": "1"};
//this is an example array, all objects have the same ID here, but not in my real array
for (var i = 0; i < 51; i++) {
arr1.push(obj);
}
var var1 = [arr1[0]["IDNumber"], arr1[1]["IDNumber"], arr1[2]["IDNumber"], arr1[3]["IDNumber"], arr1[4]["IDNumber"], arr1[5]["IDNumber"], arr1[6]["IDNumber"], arr1[7]["IDNumber"], arr1[8]["IDNumber"], arr1[9]["IDNumber"]];
//the other remaining possible variables.
var arr2 = [var1, ...]; //No clue as how to take that on
How do I do that? How do I create an array that updates itself with newer possible variables like that? Each variable must have a max of 10 objects' IDnumbers.
Suppose array1 contains your array of objects. The other one is array2 containing an array of arrays, each sub array has length 10 like you stated
You can split array1 into groups of 10 and put in array2 like this
function slice10(arraysOfObject) {
var array2 = [];
var leftOver = arraysOfObject.length % 10;
var groupsOfTen = (arraysOfObject.length - leftOver)/10;
for (var i = 0; i < groupsOfTen; i++) {
array2.push([]);
for (var j = i*10; j < i*10 + 10; j++)
array2[i].push(arraysOfObject[j]["IDNumber"]);
}
//now take care of the leftover
if (leftOver > 0) {
array2.push([]);
for (var i = groupsOfTen*10; i < arraysOfObject.length; i++)
array2[array2.length-1].push(arraysOfObject[i]["IDNumber"]);
}
return array2;
}
You could create a function to deal with adding an object to the two different data structures, and use that function also for adding the first 50 objects:
function addObject(arr1, arr2, obj) {
if (arr1.length % 10 == 0) arr2.push([]);
arr1.push(obj);
arr2[arr2.length-1].push(obj.IDNumber);
}
var arr1 = [], arr2 = [];
for (var i = 0; i < 51; i++) {
addObject(arr1, arr2, {"IDNumber": i + 1000}); // some dummy ID value
}
console.log(arr2);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Remove items with matching key from array

I have a knockout view model which contains an array of Row objects and an array of VisibleColumns objects as shown in the following screen grab:
As you can see, the VisibleCoumns array contains values which match the keys of the Rows array.
I would like to remove key value pairs of the Rows array where the key cannot be found in the VisibleColumns array.
These array are going to be quite large so I'm wondering if there's a typical javascript way of doing this kind of thing that is quite efficient?
Something like this:
for (var i = 0; i < Rows.length; i++) {
var row = Rows[i];
var keys = Object.keys(row);
for (var k = 0; k < keys.length; k++) {
if (VisibleColumns.indexOf(keys[k]) === (-1)) {
delete row[keys[k]];
}
}
}

Deleting JavaScript array element shows undefined [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(141 answers)
Closed 7 years ago.
I have a JavaScript array with objects and an array having some Ids. What I want is to compare the objects in the array with the array of ids and if any id is found in an object, I want to remove that element from the array. Doing this the result shows undefined in place of deleted element.
var data = [{"name": "John_Smith","val":"3","id":"2"},{"name": "Peter_Adams","val":"2","id":"3"},{"name": "Priya_Shetye","val":"1","id":"4"},{"name": "Sara_Brown","val":"4","id":"5"}]
var arr = ["2","5"];
for (var i = 0; i < data.length; i++) {
for(var j=0;j<arr.length;j++){
if (arr[j]==data[i].id ) {
delete data[i];
}
}
}
The result shows [undefined,object object,object object,undefined]. Is there any way to get only [object object,object object]?
use splice instead of delete
loop array from length-1 to 0, otherwise you'll miss deal with some data.
var data = [{"name": "John_Smith","val":"3","id":"2"},{"name": "Peter_Adams","val":"2","id":"3"},{"name": "Priya_Shetye","val":"1","id":"4"},{"name": "Sara_Brown","val":"4","id":"5"}];
var arr = ["2","5"];
for (var i = data.length-1; i >= 0; i--) {
for(var j = 0;j < arr.length;j++){
if (arr[j]==data[i].id ) {
data.splice(i,1);
}
}
}
console.log(data);

How to take out array from array and assign it to another array

I have an array of arrays.
I am trying to write code in javascript for following psedocode.
Take the length of array of arrays
take out each array from array and assign it to new array
like as following which is not correct.
for (var i = 0 ; i < $scope.Permissions.length; i++)
var arr + [i] = $scope.Permissions[i];
Any help?
You can also use array's builtin forEach method:
$scope.Permissions.forEach(function (arr, i) { $scope['arr' + i] = arr });
In your case it makes sense to create bunch of new arrays as properties of the $scope object:
for (var i = 0 ; i < $scope.Permissions.length; i++) {
$scope['arr' + i] = $scope.Permissions[i];
}
console.log($scope.arr0, $scope.arr1);

JavaScript: Iterating through multi-dimensional arrays and extracting multiple search values

What I want to do:
Search through a multi-dimensional array using multiple search strings.
Example of what I am currently doing:
var multiArray = [['Apples and pears', 'tuna salad'],['bananas tuna'],['the colour blue']];
var singleArray = [];
var match_array = [];
// Turn multiple arrays into one big one
for (var i = 0; i < multiArray.length; i++) {
for (var x = 0; x < multiArray[i].length; x++) {
singleArray.push(multiArray[i][x]);
}
}
// Create a new array from matched strings
function find_match(to_match) {
for (var i in singleArray) {
if (singleArray[i].indexOf(to_match)!= -1)
match_array.push(singleArray[i]);
}
return (match_array.length === 0 ? null : match_array);
}
// Find matching terms for match_array
find_match('tuna');
find_match('the');
alert(match_array);
JSFiddle Demo
Questions:
Obviously this is a cluttered way of doing this. How can this be
streamlined(i.e. searching the multiArray directly and not using
multiple find_match functions)?
How can I get only the exact string matches, preferably without breaking up the multi-dimensional array?
What are your thoughts about searching through massive
multidimensional arrays?
Do you want something like this?
var multiArray = [['Foo', 'Bar'],['Foo'],['Bar']];
var singleArray = [];
var match_array = [];
// Turn multiple arrays into one big one
for (var i = 0; i < multiArray.length; i++) {
for (var x = 0; x < multiArray[i].length; x++) {
singleArray.push(multiArray[i][x]);
}
}
// Create a new array from matched strings
function find_match(to_match, where_search) {
for (var j in where_search) {
if(where_search[j] instanceof Array){
find_match(to_match, where_search[j]);
}else{
for (var i in to_match) {
if (where_search[j].indexOf(to_match[i]) ==0 &&
where_search[j].length == to_match[i].length)
match_array.push(where_search[j]);
}
}
}
return (where_search.length === 0 ? null : match_array);
}
// Find matching terms for match_array
find_match(['Foo', 'Bar'],multiArray);
alert(match_array);

Categories