Object.values - Remove unnessary properties or extract arrays from Datatables callback object - datatables

I am generating from Datatables data object by form_data = this.api().rows().data(); and i got
form_data array of array from Datatables (image in link)
If i convert this by console.log(Object.values(form_data)); i got this
Object.values for form_data (image in link)
Is some way how remove this other properties (functions etc) inside of object and will there only arrays and this should be convert by Object.values() or should somehow extract this arrays of form_data to new array variable where will only this arrays without other properties which are inside array ???

As #andrewjames proposed... i need to add .toArray() inside my callback function in Datatables
drawCallback: function() {
form_data = this.api().rows().data().toArray();
}
after this, Datatables generate me clean Object
see image Object from Datatables only with arrays

Related

JSON Arrays with Same Identifier in React Native

I am trying to iterate through an array to grab each unique value of 5 objects that are named the same thing ("fullimage"). I will need to use the "fullimage" identifier to iterate through each image. Below is an example of the array. I've been able to display all "fullimage" objects of the array as one long string, but not a specific one only.
{"$id":"1", "images":[{"$id":"2","fullimage":"image1.jpg"}, {"$id":"3","fullimage":"image2.jpg"}, {"$id":"4","fullimage":"image3.jpg"}, {"$id":"5","fullimage":"image4.jpg"}, {"$id":"6","fullimage":"image5.jpg"}] }
I've filtered my datasource to a specific item, so in this case "filteredItem" in my code below will have filtered it to "$id":"1". This line of code below displays all "image.jpg" objects as one long string.
{this.state.filteredItem.filter(item => item.images.map(item => item.fullimage))}
This would display: image1.jpgimage2.jpgimage3.jpgimage4.jpgimage5.jpg
How about just this.state.filteredItem.images.map(item => item.fullimage)? That will return an array of strings.
If you want you can map that into an array of components.

How do I pass an array of objects into a Jade template

I have a complex array of JSON objects and I want to send that to my jade template so that so can create a visualization on the page I am rendering, but I keep having an issue with how the object is formatted.
In my controller I'm passing it like this
res.render('scatter', {
title: 'Scatter',
company: company,
graphdata: dataArray
});
in my view trying to display like this
script graphdata = "#{graphdata}";
When I log the result it looks like this
[object Object],[object Object]
when it should be an array of objects.
what am I doing wrong ?
The reason why it happens is that it tries to convert the Array into a String.
If you take an array of Objects and convert them to a string, you will get this.
(Go to browser console and do this [{a:4}, {k: 9}].toString(). And the result will be "[object Object],[object Object]".
If you want to display the array of Objects at it is, you can do:
"#{JSON.stringify(graphdata)}"

How to create an empty collection in cytoscape.js?

I want users to hide nodes in a cytoscape.js graph but also have a reset function. How should I initialize hiddenNodes in the following code? I looked at http://js.cytoscape.org/#collection/building--filtering which tells me how to add and remove nodes from a collection but not how to create an empty one.
var hiddenNodes = ?
function hide(nodes)
{
nodes.hide();
hiddenNodes = hiddenNodes.union(nodes);
}
function reset()
{
hiddenNodes.show();
}
P.S.: The hidden nodes are just a MWE, I know I could do this with selectors also.
P.P.S.: Are there alternative functions for union and difference that change the collection directly or are there only those who return new objects?
From the documentation for collection:
cy.collection() - Get an empty collection

Can't initialize custom form objects on second datatables pages

You can add static form object's like selects & inputs to datatable columns as shown here:
https://datatables.net/examples/api/form.html
But I'm trying to add custom bootstrap widgets. Like TouchSpin found here:
http://www.virtuosoft.eu/code/bootstrap-touchspin/
When I initialize TouchSpin it only initializes the objects on the first datatables page shown, the objects on the second/third pages are not initialized.
Anyone have an idea how to fix this? Its probably because the second/third pages are not part of the DOM yet?
What you need to hook into is the "drawCallback" function. You can specify it in the options when creating the DataTable. It would look something like this:
var table = $('#example').DataTable({
//... Your other options ... //
drawCallback: function(settings) {
// This callback is called when the draw is complete which happens on:
// paging, sorting, and of course when you call ".draw()". The current
// DataTable page is in the DOM with all the HTML.
// You can get the jquery element for the table like this.
var dataTableJqueryObj = $(this);
// You can get the API object like this.
var apiDataTableObj = this.api();
// Initialize your controls ...
}
});
Let me know if you have any other questions.

Iterating elements using NightWatchJS

How do i click a button returned by elements command in night watch
client.elements('xpath', ".//a[#class='abcd')]", function (allButtons){
console.log('Element value is '+element)
allButtons.value.forEach(function (element) {
this.elementIdClick(element, function(res){});
}
}
While running i am getting an error as
Element value is [object Object]
TypeError: Object #<Object> has no method 'elementIdClick'
So how do i get each element from the element list returned by client.elements
I realized the parameters for elementIdClick is wrong, i updated the code as
client.elements('xpath', ".//a[#class='abcd')]", function (allButtons){
allButtons.value.forEach(function (element) {
console.log('Element value is '+element)
this.elementIdClick(this.elementIdAttribute(allButtons.value[element].ELEMENT, 'id'), function(res){});
Now the error is
Element value is [object Object]
TypeError: Cannot read property 'ELEMENT' of undefined
So again back to original question. How do i get individual elements from a list of webelements using nightwatchJS
The following worked for me:
function iter(elems) {
elems.value.forEach(function(element) {
client.elementIdClick(element.ELEMENT)
})
};
client.elements('css selector', 'button.my-button.to-iterate', iter);
Each element is a JSON object of the form { ELEMENT: string } (so, has no method itself.)
this in forEach does not point to the element, nor client: you need to invoke client.elementIdClick() or will get a TypeError.
Hope it helps.
I used the following strategy to iterate over DOM elements using Nightwatch:
// Executing a function in the application context.
client.execute(function () {
// Get elements by CSS selector.
var elements = document.querySelectorAll('.elements');
// Iterate over them.
[].forEach.call(elements, function (element) {
// Manipulate each element.
element.click();
});
});
That snippet is inside a test of course.
If you use jQuery or something similar you can use that too.
I think the error is getting generated by your console.log() statement.
From the elements() command, allButtons.value will be an array of several objects. To access key pairs in that array, you need need to specify where in the array and then reference the object: allButtons.value[index].ELEMENT
Because you gave your .forEach() loop only one arg, it's interpreting that as the index for the array, and in my code sample below I replaced your local variable element with index for clarity. There is also no need to use the .elementIdAttribute() function; the number returned by allButtons.value[0].ELEMENT will work as the id.
client.elements('xpath', ".//a[#class='abcd')]", function (allButtons){
allButtons.value.forEach(function (index) {
console.log('Element value is '+index.ELEMENT)
client.elementIdClick(index.ELEMENT);}})
Hope that helps.