Append text with object values - arrays

I am getting object as:
{
Id:3233,
Topics:"topic",
TopicId:101,
Alreadyactiontaken:null,
…
}
But i need json format as below to pass on the api:
{
"Data":[
{
"Id":477,
Topics:"topic",
TopicId:101,
Alreadyactiontaken:null,
}
]
}
Thanks in advance

Then you just need to create such an object:
let value = {Id: 3233, Topics: "topic", TopicId: 101, Alreadyactiontaken: null, …}
let apiValue = { Data: [value] }

You can push your json data into array and assign a key for this. Like following in javascript.
var array = [];
var json = {Id: 3233, Topics: "topic", TopicId: 101, Alreadyactiontaken: null};
array.push(json);
var newdata = {};
newdata.data = array;
console.log(newdata);

Related

Store array values in object in angular

I want to store the array values in an object like below:
{"newpublicareas":[{"area_name":"x",
"level":0}]}
This is what i have done:
getNewTopLevelS(res){
this.newpublicarea = []
res.map((j)=> {
this.newpublicarea.push(j.body.data.areas)
})
this.entireareas.push(this.newpublicarea)
}
But it is pushing in wrong format.
Any suggestions?
Check this,
var res = [{"area_name":"x","level":0},{"area_name":"y","level":4},]
var newpublicarea = {newpublicarea:[]}
res.map((j)=> {
newpublicarea.newpublicarea.push(j)
})
console.log(newpublicarea)
Gives
{
newpublicarea: [{
area_name: "x",
level: 0
}, {
area_name: "y",
level: 4
}]
}
https://jsfiddle.net/1ev6tq4w/

How dynamically transform my "Object" to List in ng-model at view

I'm trying to transform my object to list dynamically, so I'm building at view instead of declaring at controller.
I don't want to declare like this: custom_fields.title_field.type_text_field = [] because the title_field is built dynamic, it could be any kind of text like full_name
My json as is:
"custom_fields":{
"title_dynamic_generate_field":{
"type_text_field":{
"name":"John",
"first_name":"Wick"
},
"type_boolean_field":{
"is_badass": true,
"is_good_movie": true
},
"type_select_field": {
"this_select": 1,
"i_got_this": "nope i didnt got this"
}
},
And to be:
"custom_fields":{
"title_dynamic_generate_field":{
"type_text_field":[{
"name":"John",
"first_name":"Wick"
}],
"type_boolean_field":[{
"is_badass": true,
"is_good_movie": true
}],
"type_select_field": [{
"this_select": 1,
"i_got_this": "nope i didnt got this"
}]
},
the object I'm trying to transform into array is type_text_field which can be dynamic too, like type_date_field or type_select_field and so on.
My ng-model is like this:
ng-model="objectApp.application.applicant.custom_fields[layout.dynamic_title][input.type][input.variable]"
the [input.type] is that I'm trying to transform into array, how can I achieve this? I tried to use $index, but got strange results.
We can do it by 2 solutions:
There is a question about your task:
? how you want handle if we have more than one type_text_field in title_dynamic_generate_field? because you want to convert it to "type_text_field":[{},...]
however my answers about the question are:
If we know what's the dynamic params which we want to send theme as json, i mean if we know what is the key of title_dynamic_generate_field or type_text_field, we do as this sample:
var data = {
"custom_fields": {
dynamicParamIs1: 'title_dynamic_generate_field',
dynamicParamIs2: 'type_text_field',
"title_dynamic_generate_field": {
"type_text_field": {
"name": "John",
"first_name": "Wick"
}
}
}
}
var paramHelper1 = json.custom_fields[json.custom_fields.dynamicParamIs1];
var paramHelper2 = json.custom_fields.dynamicParamIs2;
var solutionA = function (object, as) {
var array = [];
for (var key in object) {
var newObject = object[key];
array.push(newObject);
}
object[as] = array;
}
solutionA(paramHelper1, paramHelper2);
We changed a model of our json which can help us to detect (find) the keys
If we don't know what is the dynamic params are, we do as this:
var data = {
"custom_fields": {
"title_dynamic_generate_field": {
"type_text_field": {
"name": "John",
"first_name": "Wick"
}
}
}
}
var solutionB = function (json) {
var array = [];
for (var key in json) {
var j1 = json[key];
for (var key2 in j1) {
var j2 = j1[key2];
for (var key3 in j2) {
var fullObject = j2[key3];
array.push(fullObject);
j2[key3] = array;
}
}
}
}
solutionB(data);
This sample is manual which we use nested for to detect the keys name

Get only values from the array of array using angular js

I got an array from database as
[
{id:
{unitno: 'abc'},
amount: 100},
{id:
{unitno: 'xyz'},
amount: 150}
]
Now my required answer is that should be in the following format,
[["abc",100],
["xyz",150]]
But after some coding I got an array of Array as shown below in the console
[0:[0:"abc",1:100]
1:[0:"xyz",1:150]]
Before question down / marking duplicate please read my requirement, and if its there then mark it and please post that link as per my required solution so i can get my solution from there
So any help will be great help....
You can do like this:
data = [
{id:
{unitno: 'abc'},
amount: 100},
{id:
{unitno: 'xyz'},
amount: 150}
]
var array =[]
data.forEach(function(item){
var tmpArray = []
tmpArray.push(item.id.unitno,item.amount)
array.push(tmpArray)
})
Now you will get your required data in the array.
I don't know how you got this below code, but that's invalid Array
[0:[0:"abc",1:100] 1:[0:"xyz",1:150]]
For your desired output you can do it with JavaScript, no need angular. Just run a simple for loop.
var temp1 = [{
id: {
unitno: 'abc'
},
amount: 100
}, {
id: {
unitno: 'xyz'
},
amount: 150
}];
var eee = [];
temp1.forEach(function(el) {
eee.push([el.id.unitno, el.amount])
});
console.log('eee:', eee);
Hope this is what you needed.
var arr = [{
id : { unitno: 'abc' },
amount: 100
},{
id : { unitno: 'xyz' },
amount: 150
}];
var result = [];
for (var i = 0; i < arr.length; i++) {
var newArray = [];
newArray.push( arr[i]["id"]["unitno"] );
newArray.push( arr[i]["amount"] );
result.push( newArray );
}
console.log( result );

Mongoose array parameter filter

Let's assume I have the following model in mongoose:
var Product = new Schema({
eanCode: String,
brandName: String,
productNameNl: String,
sex: String,
suggestedRetailPrice: Number
})
How can I write a function to query this model using an array with parameters? I want a generic function which can get anything from this model using an array of filter parameters. For example:
var filterArray = [
sex: "gents",
brand: "brandName"
];
var fieldsArray = ["sex", "brand", "productNameNl", "eanCode"];
var getBrandGentsProducts = getProducts(filterArray);
function getProducts(fields, filter){
Product.Find({fields}, {filter}).exec(function(err, products){
return products;
})
}
You can reduce your fieldArray to build key value pair for your filter
Example:
// create a filterMap for your fields and value that you want to filter with
var filterMap = {
sex: "gents",
brand: "brandName"
};
var fieldsArray = ["sex", "brand", "productNameNl", "eanCode"];
var getBrandGentsProducts = getProducts(filterArray);
function mapFilter(fields, filter) {
return fieldsArray.reduce(function (obj, field) {
if (filterMap[field]) {
obj[field] = filterMap[field];
}
return obj;
}, {});
}
function getProducts(fields, filter){
var filterPair = mapFilter(fields, filter);
//console.log(filterPair) //output { "brand": "brandName", "sex": "gents" }
Product.Find(filterPair).exec(function(err, products){
return products;
})
}

Can I name a new object in an array using a variable in AngularJS?

I want to create a new nested array with the name of a variable. Is this possible?
data example:
[{
varname {
name: person1name;
events : [{
firstName: person1name,
name: event1name,
date: date1
},{
firstName: person2name,
name: event2name,
date: date2
}
]
}
}]
code:
$scope.multiEvents = []
var eventCategory = $scope.event.name
$scope.multiEvents.eventCategory.name = $scope.event.firstName;
$scope.multiEvents.eventCategory.events.push($scope.event);
$scope.multiEvents.eventCategory.events.push($scope.event2);
Ended up doing this:
$scope.multiEvents = [];
$scope.multiEvent = {};
$scope.multiEvent.events = [];
$scope.multiEvent.name = $scope.event.firstName;
$scope.multiEvent.events.push($scope.event);
$scope.multiEvent.events.push($scope.event2);
$scope.multiEvents.push($scope.multiEvent);
I think I was just confused.

Resources