I have an object and array from which I want to obtain a new object to show in the way below..
Group (Array)
this is the array and get of te services.
[
{
"IdQuestion": "5de12577fcfabc3c50660d36",
"idSurvey": "5de12546fcfabc3c50660d35",
"titleQuestion": "Porque compró el producto?",
"IdQuestionOption": "5de125b7fcfabc3c50660d39",
"idSurveyQuestion": "5de12577fcfabc3c50660d36",
"option": "Sabor"
},
{
"IdQuestion": "5de12585fcfabc3c50660d37",
"idSurvey": "5de12546fcfabc3c50660d35",
"titleQuestion": "Cómo se entero del producto?",
"IdQuestionOption": "5de125cbfcfabc3c50660d3c",
"idSurveyQuestion": "5de12585fcfabc3c50660d37",
"option": "Periodico"
},
{
"IdQuestion": "5de125a8fcfabc3c50660d38",
"idSurvey": "5de12546fcfabc3c50660d35",
"titleQuestion": "Con cuanta periodicidad consume el producto",
"IdQuestionOption": "5de125f8fcfabc3c50660d40",
"idSurveyQuestion": "5de125a8fcfabc3c50660d38", //the property is here.
"option": "Diario"
}
]
Result (Object)
this is the Object and i generate with select radios buttons with ng-models
{
"5de125a8fcfabc3c50660d38": "5de12601fcfabc3c50660d42", //the property is up.
"5de12585fcfabc3c50660d37": "5de125f0fcfabc3c50660d3f",
"5de12577fcfabc3c50660d36": "5de125c1fcfabc3c50660d3b",
"Age": "5",
"Gender": "1",
"IdSurvey": "5de12546fcfabc3c50660d35"
}
I need create a OBJECT
i need create this object for export to services
{
"Q03": "5de12601fcfabc3c50660d42",
"Q02": "5de125f0fcfabc3c50660d3f",
"Q01": "5de125c1fcfabc3c50660d3b",
"Age": "5",
"Gender": "1",
"IdSurvey": "5de12546fcfabc3c50660d35"
}
Solution 1
let res={
"5de125a8fcfabc3c50660d38": "5de12601fcfabc3c50660d42", //the property is up.
"5de12585fcfabc3c50660d37": "5de125f0fcfabc3c50660d3f",
"5de12577fcfabc3c50660d36": "5de125c1fcfabc3c50660d3b",
"Age": "5",
"Gender": "1",
"IdSurvey": "5de12546fcfabc3c50660d35"
}
res["Q03"] = res["5de125a8fcfabc3c50660d38"]
delete res["5de125a8fcfabc3c50660d38"]
res["Q02"] = res["5de12585fcfabc3c50660d37"]
delete res["5de12585fcfabc3c50660d37"]
res["Q01"] = res["5de12577fcfabc3c50660d36"]
delete res["5de12577fcfabc3c50660d36"]
Solution 2
let res={
"5de125a8fcfabc3c50660d38": "5de12601fcfabc3c50660d42",
"5de12585fcfabc3c50660d37": "5de125f0fcfabc3c50660d3f",
"5de12577fcfabc3c50660d36": "5de125c1fcfabc3c50660d3b",
"Age": "5",
"Gender": "1",
"IdSurvey": "5de12546fcfabc3c50660d35"
}
let changed={
"Q03": res["5de125a8fcfabc3c50660d38"],
"Q02": res["5de12585fcfabc3c50660d37"],
"Q01": "res["5de12577fcfabc3c50660d36"],
"Age": res.Age,
"Gender": res.Gender,
"IdSurvey": res.IdSurvey
}
Related
I'm working on a app where I retrieve json data via http, and I have made the listview.builder and all of that. Now I want to make a filter button to show the lists that only have values below a certain integer.
for example:
my list of maps goes something like this
[
{
"name": "jess",
"age": "28",
"job": "doctor"
},
{
"name": "jack",
"age": "30",
"job": "jobless"
},
{
"name": "john",
"age": "24",
"job": "doctor"
},
{
"name": "sara",
"age": "23",
"job": "teacher"
}...etc
]
Now I want to press that filter button and in my listview show only those that are below or above the age of 25.
You can map over it and add your "filter" inside the condition. Since you save age as String you have to parse it to an int:
var filteredList = myMapList.map((e){
int? parsedAge = int.tryParse(e["age"]!);
if(parsedAge != null && parsedAge >= 25){
return e;
}
}).toList();
I got the answer and it was there all the time I just didn't thought about that much.
In case anyone faces the same issue, here's how it worked for me.
list filteredList = peopleDetailsList.where((element) => int.parse(element['age'] < 25).toList();
Can someone help me with a solution to update an array object inside the MongoDB document, I've tried a couple of methods but still it's to updating, here is my document that I want to update the array in the document.
{
"title": "Products",
"description": "test",
"image": "bdd8510d75f6e83ad308d5f306afccef_image.jpg",
"_created_at": "2021-06-07T20:51:08.316Z",
"ratingCount": 0,
"ratingTotal": 0,
"placeListSave": [
{
"objectId": "g70brr45pfi",
"name": "Kale",
"email": "null",
"strBrandLogo": "84de8865e3223d1ca61386355895aa04_image.jpg",
"storeNumber": "56",
"phone": "0815342119",
"createdAt": "2021-06-10T10:19:53.384Z",
"image": "ad1fb7602c2188223fd891a52373cb9d_image.jpg"
},
{
"objectId": "0qokn33p773",
"name": "Apple",
"email": null,
"strBrandLogo": null,
"storeNumber": "01",
"phone": "011 393 8600",
"createdAt": "2021-06-11T03:11:17.342Z",
"image": "8cfcbf2bcb5e3b4ea8ade44d3825bb52_image.jpg"
}
]
}
So I only want to update the apple object and change the data, I've tried the following code but doesn't seem to work.
`
var db = client.db("test");
try {
db.collection("ShoppingCentres").updateOne({
"title": req.body.product,
"placeListSave.objectId": req.body.id,
}, {
$set: {
"placeListSave.$.email": req.body.email,
"placeListSave.$.storeNumber": req.body.storeNumber,
"placeListSave.$.phone": req.body.phone,
"placeListSave.name": req.body.name,
},
});
res.json("client");
} catch (e) {
console.log("verify", e);
}
});`
arrayFilters seems suitable here:
db.collection.update({
"title": "Products",
"placeListSave.objectId": "0qokn33p773",
},
{
$set: {
"placeListSave.$[x].email": "test#some.email",
"placeListSave.$[x].storeNumber": "test",
"placeListSave.$[x].phone": "test",
"placeListSave.$[x].name": "test"
}
},
{
arrayFilters: [
{
"x.objectId": "0qokn33p773"
}
]
})
explained:
Add array filter called "x" with the objectId for the element that you need to update and use this filter in the $set stage to update the necessary elements.
Hint: To speed up the update you will need to add index on title field or compound index on title+placeListSave.objectId
playground
I know how to operate on array of objects but never had the necessity to push one array data into another. I need to push an array of objects into another array with only 2 fields of the object. Right now my object format is somewhat like this
data: [{
"name": "Btech",
"courseid": "1",
"courserating": 5,
"points": "100",
"type": "computers"
},
{
"name": "BCom",
"courseid": "2",
"courserating": 5,
"points": "100",
"type": "computers"
}];
I want to push this into another array but I want only courseid and name in the object. I've read that we need to initialise the object in the constructor, use slice() and a few other functions and then push but I don't know how can I do it for mine since I need to push one array data into another.Kindly someone help me in this regard.
You're looking for the array map() method:
const newArray = array.map(o => {
return { name: o.name, courseid: o.courseid };
});
Try this:
let data = [{
"name": "Btech",
"courseid": "1",
"courserating": 5,
"points": "100",
"type": "computers"
},
{
"name": "BCom",
"courseid": "2",
"courserating": 5,
"points": "100",
"type": "computers"
}];
let other = []; // your other array...
data.map(item => {
return {
courseid: item.courseid,
name: item.name
}
}).forEach(item => other.push(item));
console.log(JSON.stringify(other))
// => [{"courseid":"1","name":"Btech"},{"courseid":"2","name":"BCom"}]
You can simply do it like this.
//assign your array of object to variable
var youArray:Array<any>= [{
"name": "Btech",
"courseid": "1",
"courserating": 5,
"points": "100",
"type": "computers"
},
{
"name": "BCom",
"courseid": "2",
"courserating": 5,
"points": "100",
"type": "computers"
}];
var resultArray:Array<any>=[] //empty array which we are going to push our selected items, always define types
youArray.forEach(i=>{
resultArray.push(
{
"name":i.name,
"courseid":i.courseid
});
});
console.log(resultArray)
if you still have doubts about this.please follow this url
Map to a returning JSON data, subscribe it to an existing array or an empty one. #typescript
let pictimeline= [];
var timeline = this.picService.fetchtimeline(this.limit)
.map((data : Response) => data.json())
.subscribe(pictimeline=> this.pictimeline = pictimeline);
console.log(this.pictimeline);
Hi I am implementing a chart in my Angularjs Application, You can see this plunker http://jsfiddle.net/fusioncharts/73xgmacm/ The thing which I want to achieve is to change the value attribute to profit. How can I do this ? I want to display profit not values.
Regards
After 2 days I finally find out the answer. The thing is You cannot change the Fusionchart attribute value but you can change the attribute of your API once you fetched. I used a loop after I fetched the API and replace the 'profit' attribute with value in this way I made the chart. Yes The thing which i had been ignoring was the use of 'variable' instead of scope. If you see this example you would understand Example Here. I am sharing my code May be it helps someone else too.
Give below is my json array which i called tps.json
[
{
"index": "1",
"variantoption": "fan-green",
"company": "sk fans",
"quantity": "650",
"profit": "78296",
"loss": "8457",
"year": "2016"
},
{
"index": "2",
"variantoption": "fan-white",
"company": "al ahmed fans",
"quantity": "450",
"profit": "78296",
"loss": "8457",
"year": "2016"
},
{
"index": "3",
"variantoption": "fan-purple",
"company": "asia fans",
"quantity": "350",
"profit": "78296",
"loss": "8457",
"year": "2016"
},
{
"index": "4",
"variantoption": "fan-yellow",
"company": "falcon fans",
"quantity": "250",
"profit": "78296",
"loss": "8457",
"year": "2016"
}
]
and here is my controller
$http.get('js/tps.json').success(function (data) {
var chartdata = data;
var arrLength = chartdata.length;
console.log(arrLength);
for (var i = 0; i < arrLength; i++) {
if (chartdata[i]['profit'] && chartdata[i]['index']) {
chartdata[i].value = chartdata[i].profit;
delete chartdata[i].profit;
chartdata[i].label = chartdata[i].index;
delete chartdata[i].index;
console.log(chartdata);
}
}
console.log(chartdata);
FusionCharts.ready(function () {
var tps = new FusionCharts({
type: 'column2d',
renderAt: 'chart-container',
width: '500',
height: '300',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Monthly",
"xaxisname": "Month",
"yaxisname": "Revenue",
"numberprefix": "$",
"showvalues": "1",
"animation": "1"
},
"data" : chartdata
}
});
tps.render();
});
}
);
}
-Stay foolish stay hungry
Here is the code I have. It currently returns an array full of objects. How can i return an array full of the object ids, instead?
var ref = new Firebase('https://zip-it.firebaseio.com/zips');
$scope.detectChangeDigit1 = function() {
var query = ref.orderByChild("digit1").equalTo($scope.zipCode.firstDigit.toString());
$scope.digit1Array = $firebaseArray(query);
};
A few of your JSON objects (add these to your question next time please):
"20195": {
"city": "ALTURAS",
"digit1": "9",
"digit2": "6",
"digit3": "1",
"digit4": "0",
"digit5": "1",
"population": "3969",
"state": "CA",
"zipCode": "96101"
},
"20196": {
"city": "BLAIRSDEN-GRAEAGLE",
"digit1": "9",
"digit2": "6",
"digit3": "1",
"digit4": "0",
"digit5": "3",
"population": "1434",
"state": "CA",
"zipCode": "96103"
},
You're using AngularFire, which builds on top of the Firebase JavaScript SDK. That API will always load entire nodes, it has no option to only load object IDs.
To just get the IDs, you have a few options:
keep a separate list of only the IDs and load from that.
use the REST API, which supports a shallow=true parameter
prevent the query altogether and add a list of IDs for each digit1 value
Option 1 is tricky, given that you do an orderByChild().
Option 2 will not work either, since you cannot combine shallow=true with other query parameters.
Option 3 is likely going to be most performant. You still have two sub-options in there:
Store the entire objects under digit1:
"by_digit1":
"9":
"20195": {
"city": "ALTURAS",
"digit1": "9",
"digit2": "6",
"digit3": "1",
"digit4": "0",
"digit5": "1",
"population": "3969",
"state": "CA",
"zipCode": "96101"
},
"20196": {
"city": "BLAIRSDEN-GRAEAGLE",
"digit1": "9",
"digit2": "6",
"digit3": "1",
"digit4": "0",
"digit5": "3",
"population": "1434",
"state": "CA",
"zipCode": "96103"
},
Store only the ID of each object under the "index":
"by_digit1":
"9":
"20195": true,
"20196": true,
With both these two structure, you can immediately access the list of items that you're looking for with:
ref.child('by_digit1').child('9')
With this last structure you'll then look up each city in the main list.
ref.child('by_digit1').child('9').on('value', function(snapshot) {
snapshot.forEach(function(child) {
var cityRef = ref.child('zips').child(child.key());
cityRef.once('value', function(citySnapshot) {
console.log(citySnapshot.val());
});
});
})