I am doing a Laravel project with editable inline with select option however I want I manage to query the brands and I want to display the array after I push it in the source. Please help
var brand = [];
data_brand.forEach(function(element) {
var branddetails = {value: element.id, text: element.brand_name};
brand.push(branddetails);
});
$(function(){
$('#brand').editable({
value: 2,
source: [
//I want to output like this {value: 1, text: 'Active'},
brand.values() // this code does not work
]
});
});
This should work:
source: brand.map(item => item)
or simply:
source: brand
brand is an array and you since source also expecting the array, You can try like this
source: [...brand]
In order to display the array elements, use loop.
Example-
let branddetails = [{value: 1, text: "something" }];
branddetails.forEach(brand => console.log(brand));
-- Edit --
Instead of creating array and then getting the pushed element, you can directly add the element itself in the source array.
let branddetails;
data_brand.forEach(function (element) {
branddetails = { value: element.id, text: element.brand_name };
});
$(function () {
$('#brand').editable({
value: 2,
source: [
branddetails
]
});
});
Related
I would like to make a deep copy of the below array. And the result should observe only the count property.
data = [{
id: 1,
code: 'ABC',
count: ko.observable(0)
},{
id: 2,
code: 'JKL',
count: ko.observable(5)
},{
id: 3,
code: 'QWE',
count: ko.observable(1)
}]
I tried ko.mapping.fromJS(ko.mapping.toJS(data)); but id and code was also converted to observables.
Currently I am using the below code to get the expected result.
var obj = ko.mapping.toJS(data);
var result = [];
obj.forEach(function(o){
var obs = ko.mapping.fromJS(o, {
'observe': "count"
});
result.push(obs);
});
// result will give the expected result
Here I have used forEach to loop each object in the data array. Is there a way to avoid the forEach loop ? or How to observe only the count property in array of object.
You can avoid the forEach as well as observe only count like this:
var result = ko.mapping.fromJS(data, {observe: 'count'});
console.log(result());
Edit:
According to the docs, we don't even need to mention ko.observable in our data. It should simply pick that up based on our 'observe' parameter. So maybe you might want to modify your data like this:
var data = [{
id: 1,
code: 'ABC',
count: 0
}, {
id: 2,
code: 'JKL',
count: 5
}, {
id: 3,
code: 'QWE',
count: 1
}];
And then, using forEach,
var result = [];
data.forEach(function(o){
result.push(ko.mapping.fromJS(o, {observe:'count'}));
});
console.log(result);
It's up to you, which method you would prefer to use.
I need to delete rows based on query of multiple columns, here is my code:
var destroyQuery = testArr.map(function(mc){
return {
id: mc.id,
text: mc.text
}
});
db.testModel.destroy({ where: destroyQuery }).then(function(dResponse){});
For deleting one record it works fine:
db.testModel.destroy({ where: {id: '123', text: 'abc'} }).then(function(dResponse){});
How can i delete multiple rows by querying multiple columns. Any help is appreciated.
Thanks
For your argument "mc" in function, you should use an array like given example below:
var kvArray = [{key: 1, value: 10},
{key: 2, value: 20},
{key: 3, value: 30}];
var reformattedArray = kvArray.map(function(obj) {
var rObj = {};
rObj[obj.key] = obj.value;
return rObj;
});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map check this article. might be it solve your purpose.
I used this option, is fast and simple
var destroyQuery = testArr.map(function(mc){
return {
id: mc.id,
text: mc.text
}
});
db.testModel.destroy({
where:{
[Op.or]: destroyQuery
}
}).then(function(dResponse){});
I have following 2 arrays of object. I want to convert the array from 1st format to 2nd format, i.e, data values stored from int to string, but unable to find a convinient way. Can someone help?
Please, help out with a way to do so easily by Javascript.
Try this:
The map() method creates a new array with the results of calling a provided function on every element in this array.
var test = [{
id: 0,
title: 0
}, {
id: 1,
title: 1
}, {
id: 2,
title: 2
}, {
id: 3,
title: 3
}];
var newArray = test.map(function(item) {
return {
id: item.id.toString(),
title: item.title.toString()
};
});
console.log(newArray);
Use map, for example:
tootSetArray.map(function(item){
return {
id: '' + item.id,
title: '' + item.title
};
});
The map() method creates a new array with the results of calling a
provided function on every element in this array.
I have a big json object I am using to control a cascading select list that I am trying to filter down to prepare for sending to the server.
The structure looks something like this -
Accounts[{name:account1, id:1, selected:false, schools:[{name:school1, id:2,selected:true}]}]
(With multiple accounts with multiple schools in each, keeping it simple for example purposes)
What I am trying to do is put it through some maps/filters and achieve an array of ids of schools that have the key of selected = true. So my attempt is to filter down by first all schools, then by schools that have selected true, then just the id's of those schools.
So here is my attempt -
$scope.schooIDsForSave = $scope.accountTreeHere.filter( function(obj){
return obj.schools;
}).filter( function(obj){
return obj.selected;
}).map(function(obj){
return obj.id;
});
This is only returning 1 ID so I'm getting something wrong here. I think I have something wrong with my usage of map/filter as I am still vey new to it. Any insight to point me in the right direction would be much appreciated! Thanks for reading.
Given structure
var schools = [{
name: 'account1',
id: 1,
selected: false,
schools: [{
name: 'school1',
id: 2,
selected: true
}]
}, {
name: 'account2',
id: 2,
selected: false,
schools: [{
name: 'school2',
id: 3,
selected: false
}]
}];
Try
var ids = schools.map(function(v) {
return v.schools;
}).reduce(function(a, b) {
return a.concat(b);
}).filter(function(v) {
return v.selected;
}).map(function(v) {
return v.id;
});
How to Filter a response object based on an array of whitelisted ids?
I've a working version but i don't like the nested forEach here and i wonder if there is a way to improve it?!
function WhitelistCtrl($scope) {
var visible = [];
var whitelist = [123, 456]; // items to be visible
var response = [{
id: 123,
name: 'my object #1'
}, {
id: 456,
name: 'my object #2'
}, {
id: 789,
name: 'my object #3'
}];
angular.forEach(whitelist, function (id) {
angular.forEach(response, function (item) {
if (id === item.id) {
visible.push(item);
}
});
});
}
Here is a JSFiddle to play with: http://jsfiddle.net/gearsdigital/rv6vq2L7/
I'm not much familiar with Anglar ForEeach but you can achive this using native javascript filter like bellow
visible = response.filter(function(item){
return (whitelist.indexOf(item.id) > -1);
})
DEMO
NOTE:- IE8 doesn't supports .filter.