Get only values from the array of array using angular js - angularjs

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 );

Related

how do i ask something on this website about array's

//:(this is the assigment)Updating the number of items
In the addToCart function, update the element with ID itemCounter to the length of the cartItems array.
var cartItems = [];
var backpack = {
name: "Backpack",
price: 400
}
var camera = {
name: "Camera",
price: 300
}
function addToCart(item) {
cartItems.push(item);
}
I guess you need this
var cartItems = [];
var backpack = {
name: "Backpack",
price: 400
}
var camera = {
name: "Camera",
price: 300
}
function addToCart(item) {
cartItems.push(item);
document.getElementById("itemCounter").innerHTML = cartItems.length;
}
It is pretty simle question if you know basics of JS.

Append text with object values

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);

angularjs create new json array from existing array based on id as key remaining objects are values

I have bookingData.roomList,
bookingData.roomList = [{id : <>, roomCategory : Single, number : 2}, {id:<>,roomCategory : Double, number : 1}]
And i want to make a new values like roomListData
roomListData = {<uuid> : {roomCateghory : Single, number : 2},
<uuid1> : {roomCateghory : Double, number : 2} }
please some one help me to solve this
I think This is what you need. To use a property value as property label you use []
var roomList = [{id: 4514,roomCategory: "Single", number: 2},
{id: 4542,roomCategory: "Double", number: 1}];
var newJson = [];
roomList.forEach(function(item) {
newJson.push({
[item.id] :{
"roomCategory": item.roomCategory,
"number": item.number
}
});
});
console.log(newJson);
1. using JavaScript for...in loop :
var bookingData = {
"roomList": [{
id: 1,
roomCategory: "Single",
number: 2
}, {
id: 2,
roomCategory: "Double",
number: 1
}]
};
var roomListData = {};
for(var i in bookingData.roomList) {
roomListData[bookingData.roomList[i].id] = {
"roomCategory" : bookingData.roomList[i].roomCategory,
"number" : bookingData.roomList[i].number
}
}
console.log(roomListData);
2. using Array.map() method with ES6 Arrow function :
var bookingData = {
"roomList": [{
id: 1,
roomCategory: "Single",
number: 2
}, {
id: 2,
roomCategory: "Double",
number: 1
}]
};
var roomListData = {};
bookingData.roomList.map(item =>
roomListData[item.id] = {
"roomCategory" : item.roomCategory,
"number" : item.number
});
console.log(roomListData);
3. using ES6 Spread assignment :
var bookingData = {
"roomList": [{
id: 1,
roomCategory: "Single",
number: 2
}, {
id: 2,
roomCategory: "Double",
number: 1
}]
};
function createObj(a,b) {
var roomListData = {};
roomListData[a.id] = a,delete a.id;
roomListData[b.id] = b,delete b.id;
return roomListData;
}
var res = createObj(...bookingData.roomList);
console.log(res);
`
var roomList = [{id : 1, roomCategory : "Single", number : 2}, {id:2,roomCategory : "Double", number : 1}];
var roomListData = {};
for(var i=0;i< roomList.length ;++i){
var temp = roomList[i];
var id = temp.id;
delete temp.id;
roomListData[id]=temp;
}
console.log(roomListData);
`
I assuming by uuid1 you mean a new uuid, not 'uuid' with the number 1 appended.
bookingData.roomList = [{id : <>, roomCategory : Single, number : 2}, {id:<>,roomCategory : Double, number : 1}]
roomList = {};
bookingData.forEach(function(element, index, array) {
var uuid = GenerateNewUUID();
// or uuid = element.id;
delete element.id;
roomList[uuid] = element;
});

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.

can not extract info from $scope in angularjs

I have a code as follow:
function getdata($scope){
$scope.todos = [
{name:"john",data:"1 2 3"},
{name:"Marry",data:"6 7 8"},
{name:"Edward",data:"2 4 5"}
];
var seri=new Array();
for(var item in $scope.todos)
{
seri.push(
{
name: item.name,
data: [1, 0, 4]
});
}
console.log(seri);
}
Now when I check the console for the name it returns undefined. what is the problem?
Try
function getdata($scope) {
$scope.todos = [
{ name: "john", data: "1 2 3" },
{ name: "Marry", data: "6 7 8" },
{ name: "Edward", data: "2 4 5" }
];
var seri = new Array();
angular.forEach($scope.todos, function (item, key) {
seri.push({
name: item.name,
data: [1, 0, 4]
});
});
console.log(seri);
//you can get data from this by using return
}
When you loop through an array in javascript the item is an integer with the position of the value in the array. You'll need to use this position to get the true item, like this:
for (var i in $scope.todos) {
var item = $scope.todos[i];
...
}
If the function is inside a controller and being called as such you should declare it as
$scope.getData = function() {
}
And then it should work fine. If the function is NOT to be part of the scope and you are passing the scope in then try receiving the parameter as "scope" and not "$scope" which has a special meaning in Angular.
this is not how you iterate array in javascript (see materik answer).
you can alternatively use angular.forEach to iterate the todos array like this:
angular.forEach($scope.todos, function(item)
{
seri.push(
{
name: item.name,
data: [1, 0, 4]
});
});

Resources