I have a service that i am using to apply a unique number to a model name. the result i am getting is this
"sectionInputs": [
{
"model": "price_min1"
},
{
"model": "price_max2"
},
{
"model": "units_occ3"
},
{
"model": "inv_mod4"
},
{
"model": "inv_fin5"
},
{
"model": "inv_vdl6"
},
{
"model": "inv_uc7"
},
{
"model": "inv_fut8"
},
{
"model": "inv_con9"
},
{
"model": "units_total10"
}
]
i need this to each have '1'. and then in the next object array i need them to have '2', etc... as of now every object array looks like this. i have a plunker with everything setup.
plunker
function sectionInputSvc(sectionInputs) {
var vm = this;
vm.sectionInputsArry = sectionInputs;
vm.sectionInputs = function () {
var arry = [];
var counter = 0;
for (var i = 0; i < vm.sectionInputsArry.length; i++) {
counter++
var obj = {
model: vm.sectionInputsArry[i].model + counter
};
arry.push(obj);
};
return arry;
};
};
[EDIT 2]
in app.js...
sections[i].sectionInputs = sectionInputSvc.sectionInputs(sections[i],i);
and in section.service.js...
function sectionInputSvc(sectionInputs) {
var vm = this;
vm.sectionInputsArry = sectionInputs;
var obj2={};
vm.sectionInputs = function (obj2,num) {
var arry = [];
var counter = 0;
for (var i = 0; i < vm.sectionInputsArry.length; i++) {
counter++
var obj = {
model: vm.sectionInputsArry[i].model + num
};
arry.push(obj);
};
return arry;
};
};
Using linq.js, and assuming the suffixes do not already exist on the names:
vm.sectionInputs = function () {
return Enumerable.From(vm.sectionInputsArry).Zip(Enumerable.ToInfinity(1),
"{ model: $.model+$$ }"
).ToArray();
};
Related
I have an array of objects (pre_finalTab_new below) like this:
My goal is to group them by "schema", and then by "tip" and insert into new array, something like this:
var celotnaTabela = {};
for (var nov in pre_finalTab_new)
{
var shema = pre_finalTab_new[nov].schema.trim();
var objekt_tip = pre_finalTab_new[nov].type.trim();
var objekt_name = pre_finalTab_new[nov].name.trim();
var tip = pre_finalTab_new[nov].tip.trim();
if (celotnaTabela[shema] === undefined)
{
celotnaTabela[shema] = [];
if (celotnaTabela[shema][tip] === undefined)
{
celotnaTabela[shema][tip] = [];
if (celotnaTabela[shema][tip][objekt_tip] === undefined)
{
celotnaTabela[shema][tip][objekt_tip] = [];
celotnaTabela[shema][tip][objekt_tip] = [objekt_name];
} else
celotnaTabela[shema][tip][objekt_tip].push(objekt_name);
}
} else
{
if (celotnaTabela[shema][tip] === undefined)
{
celotnaTabela[shema][tip] = [];
}
if (celotnaTabela[shema][tip][objekt_tip] === undefined)
{
celotnaTabela[shema][tip][objekt_tip] = [];
celotnaTabela[shema][tip][objekt_tip] = [objekt_name];
} else
{
if (!celotnaTabela[shema][tip][objekt_tip].includes(objekt_name))
celotnaTabela[shema][tip][objekt_tip].push(objekt_name);
}
}
}
Then if i output celotnaTabela, i got this:
Expanded:
Even more:
But the problem is, when i try to use JSON.stringify(celotnaTabela), i got this:
{"HR":[],"ZOKI":[]}
But i need it to be in a right format, so i can pass this object into AJAX call..
Can anyone help me with this, what am i doing wrong?
i hope i understood everything right you asked for.
Next time provide the testdata and the wished result in textform pls.
var obj = [
{ schema: "HR", type: " PACKAGE", name: "PAKET1", tip: "new_objects" },
{ schema: "HR", type: " PACKAGE", name: "PAKET2", tip: "new_objects" },
{ schema: "HR", type: " PROCEDURE", name: "ADD_JOB_HISTORY", tip: "new_objects" },
{ schema: "ZOKI", type: " TABLE", name: "TABELA2", tip: "new_objects" },
{ schema: "ZOKI", type: " TABLE", name: "TABELA3", tip: "new_objects" },
];
var out = {};
for (var i = 0, v; v = obj[i]; i++) {
var a = v.schema.trim();
var b = v.type.trim();
var c = v.tip.trim();
var d = v.name.trim();
if (!out.hasOwnProperty(a)) {
out[a] = {};
}
if (!out[a].hasOwnProperty(b)) {
out[a][b] = {};
}
if (!out[a][b].hasOwnProperty(c)) {
out[a][b][c] = []
}
out[a][b][c].push(d);
}
console.log(JSON.stringify(out, null, 2));
I want to sum total depending for each department without duplicates.
Yesterday user #yBrodsky helped me with that and suggested to use SQL but today I want to add a couple options and I can't do this in sql.
Main problem:
I have an array like:
var data = [{
dept:"test",
task_time:"83"
},{
dept:"test",
task_time:"41"
},{
dept:"other",
task_time:"10"
}];
And I want to sum task_time for every dept: for example test = 124 and other = 10.
There is a function which should calculate it but it works like test = 83. test = 41 and other = 10. And shows every dept instead one with sum.
There is function.
var totalPerDept = {};
angular.forEach(data, function(item) {
if(!totalPerDept[item.dept]) {
totalPerDept[item.dept] = 0;
}
totalPerDept[item.dept] += parseFloat(item.task_time);
});
Here's the above as a snippet:
var data = [{
dept:"test",
task_time:"83"
},{
dept:"test",
task_time:"41"
},{
dept:"other",
task_time:"10"
}];
var totalPerDept = {};
angular.forEach(data, function(item) {
if(!totalPerDept[item.dept]) {
totalPerDept[item.dept] = 0;
}
totalPerDept[item.dept] += parseFloat(item.task_time);
});
console.log(totalPerDept);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
I did some modifications to your code to obtain an array with the result you want:
function sumDeptData() {
var data = [{
dept:"test",
task_time:"83"
},{
dept:"test",
task_time:"41"
},{
dept:"other",
task_time:"10"
}];
var totalPerDept = [];
angular.forEach(data, function(item) {
var index = findWithAttr(totalPerDept, 'dept', item.dept);
if (index < 0) {
totalPerDept.push({
dept: item.dept,
total: parseFloat(item.task_time)
});
} else {
totalPerDept[index].total += parseFloat(item.task_time);
}
});
return totalPerDept;
}
function findWithAttr(array, attr, value) {
for(var i = 0; i < array.length; i += 1) {
if(array[i][attr] === value) {
return i;
}
}
return -1;
}
sumDeptData() returns [{"dept":"test","total":124},{"dept":"other","total":10}]
Here is an example using Plain JavaScript.
var data = [{
dept: "test",
task_time: "83"
}, {
dept: "test",
task_time: "41"
}, {
dept: "other",
task_time: "10"
}],
minArr = [];
data.forEach(function(v) {
if (!this[v.dept]) {
this[v.dept] = {
dept: v.dept,
task_time: 0
};
minArr.push(this[v.dept]);
}
this[v.dept].task_time = parseInt(this[v.dept].task_time) + parseInt(v.task_time);
}, {});
console.log(minArr);
I am trying to get a list of tags from an object
Here is the full code and data:
$(document).ready(function() {
obj =
{
"category":[
{
"id":"8098",
"tags":{
"411":"something",
"414":"something else"
}
}
]
};
var tagList = [];
for (var index in obj) {
for (var indexSub in obj[index].tags) {
blah = (obj[index].tags[indexSub]);
tagList.push(blah);
}
}
console.log(tagList);
});
Here's a link to jsFiddle
The problem is that taglist is returning an empty array.
How can I fix this?
Yours
for (var index in obj) {
for (var indexSub in obj[index].tags) {
blah = (obj[index].tags[indexSub]);
tagList.push(blah);
}
}
New to get the tags that are into the category
for (var index in obj) {
for (var indexSub in obj[index]) {
blah = (obj[index][indexSub].tags);
tagList.push(blah);
}
}
See the diference in the way to access the properties
obj =
{
"category": [
{
"id": "8098",
"tags": {
"411": "something",
"414": "something else"
}
}
]
};
var tagList = [];
for (var index in obj) {
for (var indexSub in obj[index]) {
tagList = (obj[index][indexSub].tags);
// tagList.push(blah);
}
}
console.log(tagList);
You need to iterate the category, and then get the keys and values of tags, somethig like this..
var obj =
{
"category":[
{
"id":"8098",
"tags":{
"411":"something",
"414":"something else"
}
}
]};
var tagList = [];
obj.category.forEach( o =>{
for (property in o.tags){
tagList.push(property + ':'+ o.tags[property]);
};
});
console.log(tagList);
Here is your jsfiddle
obj =
{
"category":[
{
"id":"8098",
"tags":{
"411":"something",
"414":"something else"
}
}
]
};
var tagList = [];
for (key in obj) {
for(subKey in obj[key]){
var myTags=obj[key][subKey].tags;
for(tag in myTags){
tagList.push(tag);
}
}
}
console.log(tagList);
You could use Array#reduce and Object.keys with Array#map for iterating and getting all tags.
var obj = { "category": [{ "id": "8098", "tags": { "411": "something", "414": "something else" } }] },
tagList = obj.category.reduce(function (r, a) {
return r.concat(Object.keys(a.tags).map(function (t) { return a.tags[t]; }));
}, []);
console.log(tagList);
Below is the JSON data.
JSON :
[{
"Code":"US-AL",
"Name":"Alabama",
"Population":4833722
},
{
"Code":"US-AK",
"Name":"Alaska",
"Population":735132
},
{
"Code":"US-AZ",
"Name":"Arizona",
"Population":6626624
},
{
"Code":"US-AR",
"Name":"Arkansas",
"Population":2959373
},
{
"Code":"US-CA",
"Name":"California",
"Population":38332521
},
{
"Code":"US-CO",
"Name":"Colorado",
"Population":5268367
},
{
"Code":"US-CT",
"Name":"Connecticut",
"Population":3596080
}]
I wanted to convert that data to this format.
[{
"US-AL": {
"name" : "Alabama",
"population" : 4833772
},
"US-AK": {
"name" : "Alaska",
"population" : 735132
}
}]
I tried with this function and separated the name and population from it.
var ParentData = [];
var ChildData = {"name": [], "population": []};
data.forEach(function(val, i) {
ParentData.push(val.Code);
ChildData.name.push(val.Name);
ChildData.population.push(val.Population);
})
But I'm not that expert in this. Just a learner and I don't know how to push to the parent data which gets aligned to it respectively.
Any help will be very much helpful for me to achieve this.
Thanks in advance.
Try this:
var newData = {};
data.forEach(function(val) {
newData[val.Code] = {name: val.Name, population: val.Population};
});
Keep in mind that forEach isn't natively supported by IE8-, although it can be polyfilled. This works in every browser:
for (var i = 0; i < data.length; i++)
newData[data[i].Code] = {name: data[i].Name, population: data[i].Population};
Or, since you added the "jquery" tag, you can also use:
$.each(data, function() {
newData[this.Code] = {name: this.Name, population: this.Population};
});
Try with native map of javascript
var newData = data.map(function (obj) {
var newObj = {};
newObj[obj.Code] = {};
newObj[obj.Code].name = obj.Name;
newObj[obj.Code].population = obj.Population;
return newObj;
});
console.log(newData)
[{
"US-AL":{
"name":"Alabama",
"population":4833722
}
},
{
"US-AK":{
"name":"Alaska",
"population":735132
}
},
{
"US-AZ":{
"name":"Arizona",
"population":6626624
}
},
{
"US-AR":{
"name":"Arkansas",
"population":2959373
}
},
{
"US-CA":{
"name":"California",
"population":38332521
}
},
{
"US-CO":{
"name":"Colorado",
"population":5268367
}
},
{
"US-CT":{
"name":"Connecticut",
"population":3596080
}
}
]
And the code:
var newDatas = datas.map(function(item) {
var obj = {};
obj[item.Code] = { name: item.Name, population: item.Population };
return obj;
});
datas is the array containing your original source.
I've the current JSON that looks like this
{
"_id": "5371f7171b27d11418bd9b46",
"name": "a",
"length": 14
},
{
"_id": "5371f7171b27d1143bd9b46",
"name": "ab",
"length": 3
}
and I'm trying to get it to look like this one,
5371f7171b27d11418bd9b46: {
name: "a",
length: 14,
toc: [] // in the future it will be in the json I get.
},
5371f7171b27d1143bd9b46: {
name: "ab",
length: 3,
toc: []
}
this is what I tried,
var arrenged = {};
_.forEach(result, function(item) {
arrenged[item._id] = arrenged[item._id] || {};
arrenged[item._id][item.name] = arrenged[item._id][item.name] || {};
arrenged[item._id][item.name].push(item);
arrenged[item._id]['direction'] = 'rtl';
arrenged[item._id]['toc'] = "[]";
});
res.jsonp(arrenged);
but I get TypeError: Object # has no method 'push', not sure what I'm doing wrong.
thanks!
Could just do:
var newObject = {};
for (var i = 0; i < data.length; i++) { //data is your current array of objects
var id = data[i]._id;
newObject[id] = {};
newObject[id].name = data[i].name;
newObject[id].length = data[i].length;
newObject[id].toc = [];
}
Fiddle: http://jsfiddle.net/KWjb3/