hi this is my array
this.testArray = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }];
i want to check whether officename sun is there or not. I tried used include
this.testArray.includes("sun")
but this will work for one dimension array( it will return true). how if I want to use it for this multidimension array. it always return false.
** this.testArray.includes("sun") This will work with this array**
this.testArray = ["sun","moon","stars" }];
it will return true it exist. but it is not working with my array the up one.
Your array is not multidimensional. However you can do it like :
testArray.find(function(i){
return (i.officename === 'sun');
});
In case you need a filter directly
| filter:{officename:'sun'}
or negative
| filter:{officename:'!sun'}
Simply you can check value include or not by using underscorejs find function
this.testArra = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }]
var temp = _.find(this.testArra, function (o) { return o.officename==='sun2'; })
if temp "undefined" value not include, else value include
you can use find method in javascript
ES6 demo
var testArray = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }];
var arr = testArray.find(o=> o.officename === "sun");
console.log(arr)
ES5 demo
var testArray = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }];
var arr = testArray.find(function(o){
return o.officename === "sun"
});
console.log(arr)
Related
I am developing the react native application and I want to split the array but I can't understand how to do that,
[{"dtCreatedOn": "2021-06-01T03:28:21.450Z", "flgIsActive": true, "inTagId": 2, "stTags": "Song"}, "3", "6", "7", "8"]
I have something like the above array, I want to get the value of the inTagId and also last integer value "3", "6", "7", "8" from this array
var a = [{"dtCreatedOn": "2021-06-01T03:28:21.450Z", "flgIsActive": true, "inTagId": 2, "stTags": "Song"}, "3", "6", "7", "8"]
var tagID = a[0].inTagId
var b = Object.keys(a)
var lastInteger = b[(b.length-1)]
var test = [{"dtCreatedOn": "2021-06-01T03:26:44.910Z", "flgIsActive": true, "inTagId": 1, "stTags": "Emotion"},"4","5","6"]
var tags = [];
test.map(function(ele) {
if(typeof ele === 'object')
{
tags.push(ele.inTagId);
}
else
{
tags.push(ele)
}
});
tags.join(',')
This is the code to get the details from my table.
function listshops(callback)
{
var array=[3,4];/*shopId*/
async.each(array,function(dat,callback){
async.parallel([
function(callback){
client.connection.query('select * from shop where shopId=?',dat,function(err,data1){
callback(null,data1);
});
},
function (callback) {
client.connection.query('select * from image where shopId=?',dat,function(err,data2){
callback(null,data2);
});
}
],
function(err,data)
{
var result = data.reduce(function(prev, curr) { /*merging the array*/
return prev.concat(curr);
});
console.log(result);
});
});
}
I got an output like this:: http://i.stack.imgur.com/NVUAu.png
i want to print my result in the below format:
{
"shops": [
{
"shopId": "3",
"shopName": "1",
"address": "abc",
"contactNumber":"1234"
"images": [
{
"imageId": "1",
"shopId": "3",
"imageUrl": "aaa",
},
{
"imageId": "2",
"shopId": "3",
"imageUrl": "bbb",
},
]
},
{
"shopId": "4",
"shopName": "2",
"address": "bbb",
"contactNumber":"1234"
"images": [
{
"imageId": "3",
"shopId": "4",
"imageUrl": "ccc",
},
{
"imageId": "4",
"shopId": "4",
"imageUrl": "ddd",
},
]
},
]
I got the values but some confusions in fetching the values.
You have two nested async tasks here, parallel and each.
Parallel takes care of getting the shop information and the images for one shop and calls the final callback with a two element array that has the results for your tasks.
This is your final callback for parallel:
function(err,data)
{
var result = data.reduce(function(prev, curr) { /*merging the array*/
return prev.concat(curr);
});
console.log(result);
});
data should be a two element array, where element 0 is the shop, and element 1 are the images. You just concatenate these together. If you want your images in the desired format, you should do data[0].images = data[1] to add the images key to your shop.
That so far is for one shop. Then there is the outer each loop. You are currently not giving this a final callback and not doing anything with the result.
Something like this:
function listshops(callback)
{
var array=[3,4];/*shopId*/
async.each(array,function(dat,eachCallback){
async.parallel([
function(parallelCallback){
client.connection.query('select * from shop where shopId=?',dat,function(err,data1){
parallelCallback(null,data1);
});
},
function (parallelCallback) {
client.connection.query('select * from image where shopId=?',dat,function(err,data2){
parallelCallback(null,data2);
});
}
],
// this is the final callback for parallel
function(err,parallelResult)
{
// construct one shop
var shop = parallelResult[0];
shop.image = parallelResult[1];
// pass the shop info back as a result to each
eachCallBack(shop);
});
},
// this is the final callback for each
function(err, eachResult) {
// eachResult is an array with the shops returned from parallel callback
var result = {
shops: eachResult
}
return result
}
);
}
I couldn't test this, so don't consider it an exact solution, but an explanation. I renamed your variables for better understandability, you don't have to do that in your code. The key thing to keep in mind is that you have two nested tasks here, like a nested loop and you also have to deal with the results on two levels.
so I have chatting application, with this JSON:
{
"561c": [{
"from": "561c",
"fromname": "ryan",
"to": "sasa",
"messgae": "hey"
}, {
"from": "5512",
"fromname": "sasa",
"to": "ryan",
"messgae": "hey too"
}]
}
but this JSON will always add up when the users send messages. I want to take the the last value just from "message" to use this value in my Text-to-Speech code, how do I write the code?
and this is my Text-to-Speech:
$scope.speakText = function() {
TTS.speak({
text: ***this place is for the code***,
locale: 'en-GB',
rate: 0.75
}, function () {
// handle the succes case
}, function (reason) {
// Handle the error case
});
};
use forEach loop on the object '561c' like
var messArray = [];
561c.forEach(function(obj){
messArray.push(obj.message)})
var text = messArray.join();
You will have all the message in messArray.
If i have understood your question correct.
//get the last element of array
var lastIndex = 561c.length();
var lastObj = 561c[lastIndex];
//get message from last object of array 561c
var lastMessage = lastObj.message;
and you got what you want(y);
You can use the "pluck" function of underscore.js - http://underscorejs.org/#pluck
_.pluck(your array of JSONs, 'messgae');
You can pass $scope to your function then pass 561c you will get object then you can index message in it
Example :
$scope.chat = {
"561c": [{
"from": "561c",
"fromname": "ryan",
"to": "sasa",
"messgae": "hey"
}, {
"from": "5512",
"fromname": "sasa",
"to": "ryan",
"messgae": "hey too"
}]
}
angular.module('app',[]).controller('myctrl', function($scope, data){
$scope.561c = data.messgae;
}
I have a input where I want to find an object by ID. At the moment I am returning both objects but what I want is if I search '01' I would just return the first object. I have tried underscore _.map to do this but it did not give the result I am after.
var getById = function() {
var deferred = Q.defer(),
result;
result = items;
if (!result) {
deferred.reject('item not found');
} else {
deferred.resolve(result);
}
return deferred.promise;
};
JSON:
[{
"id": "01",
"name": "test1",
"orderItems": [
{
"productNumber": "TESTa",
"quantity": 2,
},
{
"productNumber": "TESTb",
"quantity": 4,
},
{
"productNumber": "TESTc",
"quantity": 6,
}
]
},{
"id": "02",
"name": "test2",
"orderItems": [
{
"productNumber": "TESTe",
"quantity": 2,
},
{
"productNumber": "TESTf",
"quantity": 7,
},
{
"productNumber": "TESTg",
"quantity": 6,
}
]
}]
You can use _.filter()
Looks through each value in the list, returning an array of all the values that pass a truth test (predicate).
result = _.filter(items, function(item){
return item.id == '01';
});
Or, _.findWhere
Looks through the list and returns the first value that matches all of the key-value pairs listed in properties.
result = _.findWhere(items, {id : '01'});
var result = _.find(myItems, function(item) { return item.id === '01'; }
If you find single item which matches the conditions, use _.find()
It looks through each value in the list, returning the first one that
passes a truth test
var _exist = _.find(_items, function (item) {
return item.id == your_id;
});
If you find all items which matches the conditions, use _.filter()
It looks through each value in the list, returning an array of all the
values that pass a truth test
var _exist = _.filter(_items, function (item) {
return item.id == your_id;
});
Catch the complete documentation here:
http://underscorejs.org/
I apologise if this has been answered already, but I'm new to Angular so might have missed it.
I have a need to provide multiple sets of checkbox lists, which need to be combined to create an AND query.
It's on Plunker here http://plnkr.co/OGmGkz22n4J4T8p74yto but enclosed below. At the moment I can select the bottom row and the correct names appear from storeArray, but I cannot work out how to add the Format array into the filter.
I've tried:
<div ng-repeat="store in storeArray | filter:(formatFilter && tillFilter)">
and
<div ng-repeat="store in storeArray | filter:formatFilter:tillFilter">
but they don't work.
Any suggestions please?
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.formatFilter = function(a) {
for (var fmt in $scope.formatsArray) {
var f = $scope.formatsArray[fmt];
if (f.on && a.format.indexOf(f.name) > -1) {
return true;
}
}
};
$scope.tillFilter = function(a) {
for (var till in $scope.tillsArray) {
var t = $scope.tillsArray[till];
if (t.on && a.tills.indexOf(t.name) > -1) {
return true;
}
}
};
$scope.formatsArray = [{
name: "Super",
on: false
}, {
name: "Express",
on: false
}, {
name: "Other",
on: false
}];
$scope.tillsArray = [{
name: "Main",
on: false
}, {
name: "Service",
on: false
}, {
name: "Petrol",
on: false
}];
$scope.storeArray = [{
"id": "1",
"name": "101",
"format": "Super",
"tills": ["Main", "Service", "Petrol"]
}, {
"id": "2",
"name": "102",
"format": "Express",
"tills": ["Main", "Service"]
}, {
"id": "3",
"name": "103",
"format": "Other",
"tills": ["Main", "Petrol"]
}, {
"id": "4",
"name": "104",
"format": "Super",
"tills": ["Service", "Petrol"]
}];
}
While you can chain filters together like this:
<div ng-repeat="store in storeArray | filter:formatFilter | filter:tillFilter)">
This won't fix your problem since the first filter will do it's job, and filter items out that you may want to include in your second filter. I'm not sure of any way to do an "or" filter. Is there any reason you can't do a custom filter that includes both? I modified your plunker with a custom filter:
http://plnkr.co/edit/han1LFl7toTsSX27b9Q0?p=preview
The code isn't super clean... it does the job. :) You may want to polish it up a bit.