vuejs make array of objects data - javascript

I have array of data (selected items) and I need to extract ids of this array into new array so I can send those ids only to back-end.
Code
method
toggleSelection(rows) {
console.log('this.multipleSelection : ',this.multipleSelection); // prints my default array (include all data)
this.multipleSelection.forEach(row => {
console.log('rows: ', row) // get each object of array (extract ids here)
// send axios request to backend (ids only)
});
},
Screenshot
here is result of console codes above
any idea?

At first I need to say I never worked with Vue.js. But in simple vanilla-javascript you could use the map function. I don't know if this works but Here is a possible answer:
yourids = this.multipleSelection.map(row => row.id);

Related

How to sort the relations returned using get work items API?

I am writing a TFS extension in javascript where I am using the 'GetWorkItem' function within 'TFS/WorkItemTracking/RestClient' library.
wiRestClient.getWorkItem(<workItemID>, null, null, Contracts.WorkItemExpand.All)
.then(function success(workItem) {
console.log(workItem);
});
the output generated by the code above is as below:
This PBI has about 40 tasks within it and they are fetched in random order by the API.
Is there a way that these relations are fetched in the order of their Id?
I process the relations returned in the result, fetch the Id from a forward relation, get the workItemId
and add it to an array.
Now, this array has information about all the child workitems of the parent PBI.
I tried to sort this array based on System.Id in the fields property.
This is the function I use to sort the data:
childWorkItems.sort(function(a,b) {
return a["System.Id"] > b.["System.Id"]
});
console.log(childWorkItems);
This doesn't seem to work. The array is still in random order.
I solved by changing the sort function to
childWorkItems.sort(function(a,b) {
return a["System.Id"] - b.["System.Id"]
});
console.log(childWorkItems);

How do i pass a multidimensional array to my c# web api

I have a very large array that looks like this:
So in this case it consist of 85 arrays with 473 counts of data in each array.
I am currently passing this array to a web api to then process this data, this is how i do it:
function DeepDataProcess(array, callback) {
console.log(array[0]);
console.log(array);
var promise = HttpRequestFactory.Go({
url: 'api/ImportData/PostDataProcessing',
method: 'POST',
data: array[0] // this is the first index in the array I want to send the full array.
});
promise.then(function (response) { callback(response); }).catch(function (err) { console.log(err); });
}
as you can tell in my javascript i am only passing one array because when i try to send the full array my web api says my parameter is null. I also don't want to call my web api 85 times to get the full array in there, note: i know this array is a multidimensional array.
and below is my web api (c#):
[Route("PostDataProcessing")]//I have tried adding string[,] as the parameter
public IHttpActionResult PostDataProcessing(string[] array) //so that it can accept a multidimensional array but I still get a null value
{
var response = setDataProcessorService().DeepDataProcess(array);
return Ok(response);
}
when i send the full array my web api says my parameter array is empty.
How do I make my c# web api accept a multidimensional array as its parameter?
Below is the request I am making:

Angular $http.query use array as params?

I have an Angular 1 application where a user can select several items by clicking on a checkbox. When he clicks a particular button, these Ids are aggregated as this:
angular.forEach($scope.orders, function (order, id) {
if (order.export) {
ids.push(order.id);
}
});
Now I want to query these Ids in my php api action, but how can I transmit all id's to my apiAction? I tried it with $http.query ($scope.items = Items.query({ 'ids' : ids});), but this does not work as it creates a strange url which can not be parsed.
Then I tried it with: $http.save and there I can transfer the ids to my api action, but I also need to return the result back to my application in order to show it on the frontend, but with using $http.save this does not seem to be possible, as I always get a orderBy:notarray error.
So how can I use $http.save and get a proper array in return or use $http.query with an array with multiple ids in the params?
Thanks
In angular $http call...
params: {
id: JSON.stringify(ids)
}
You can convert your array into comma separated string and then pass that string into your php api and then you can get back your array in php. E.g.,
Angular side,
ids.toString()
PHP side,
explode(",",$query_param);

Jquery Get array from json and see if value exists

I'm trying to write a script that retrieves data from JSON using jquery. I searched the downloaded array. How to find the value to perform any operation.
I pull out the data using getJSON and everything works. But I'm having trouble finding the search. Can anyone help me? This data from JSON:
[{"id":"10","data":"12345"},{"id":"11","data":"6666"}]
The simplest way to search through an array would be....
var data = [{"id":"10","data":"12345"},{"id":"11","data":"6666"}];
for(var row in data)
{
var item = data[row]; //This is the object {id:..., data:...}
if(item.id == ...) //Use if statement to search for data
{
//do something here when data is found
}
}

How to query Firebase data after using .push() to add data?

Here is the code for when I'm pushing the data to Firebase:
firebase.database().ref(`booklogs/${uid}/${book_id}`).push(page_id)
booklogs :
{HUMjSHxVKAPfVXzOId9zCBkGOgv1:{
book28917: {
-KYp4FdYYODDZG1FX-Pb: 1
}
}
}
My problem is when I query the data, the child node of the ${book_id} includes the push key, but I only want to get the value which is 1 and not the push key.
The code I use to query is:
var booklogs = db.ref(`booklogs/${uid}/${project}`);
booklogs.once('value')
.then((snapshot) => {
console.log(`pages viewed are ${snapshot.key}: ${snapshot.val()}`);
console.dir(snapshot.val());
}).catch((error) => {
console.log(`Error : ${error}`);
});
The data returned in the console is:
pages viewed are 2634651: [object Object]
{ '-KYp4FdYYODDZG1FX-Pb': 1 }
Any input would be much appreciated. Thanks!
If you only want the '1' and not the push key, try using .set()
firebase.database().ref(`booklogs/${uid}/${book_id}`).set(page_id)
That will get rid of the object and just give you the value that you wanted. Push automatically generates a key for every value you add, so you will always get an object back. From the Firebase docs - "For basic write operations, you can use set() to save data to a specified reference, replacing any existing data at that path."
https://firebase.google.com/docs/database/web/read-and-write

Categories

Resources