2

this is my object i want to select value of quantity and type i don't know how if i have to make a matrix then place every value in the martix cell am a begginer in AngularJs , Thank you

[
    {
        id: 15,
        nom: "azerty",
        type: "azerty",
        quantity: 456,
        prix: 25296
    },
    {
        id: 21,
        nom: "sdqs",
        type: "qsdqsd",
        quantity: 102,
        prix: 52
    }
]
5
  • Once you select quantity, what do you want to do with it? Do you want to add it to your scope? Show it in your view? Can you post more of your code/give more context? Commented Feb 23, 2016 at 9:50
  • actually i want to use the values in a chart Commented Feb 23, 2016 at 9:52
  • for now all i want to see is the values in console.log() Commented Feb 23, 2016 at 9:54
  • can you post an example array showing how you want the results Commented Feb 23, 2016 at 9:59
  • var quanity = [456, 102]; and var type = ["azerty","qsdqsd"] Commented Feb 23, 2016 at 10:02

4 Answers 4

1

Do you want something like this:

var initialObject = [
 {
  id: 15,
 nom: "azerty",
 type: "azerty",
   quantity: 456,
 prix: 25296
},
{
 id: 21,
 nom: "sdqs",
  type: "qsdqsd",
 quantity: 102,
 prix: 52
 }
 ];

var newArray = {};

var createNewArray = function() {
    angular.forEach(initialObject, function(value, key) {
       newArray.push(value.quantity);
       newArray.push(value.type);
    })
}

//Should produce {456, azerty, 102, qsdqsd}

So if you just wanted a quantity array, just change newArray to quantity, and remove newArray.push(value.type);. Similar thing with type.

Sign up to request clarification or add additional context in comments.

Comments

1

It seems your problem has nothing to do with AngularJS itself. You can transform data using lodash or underscore. Here is an example built using lodash (btw, I added one more array element to show how groupBy works):

var docs = [
    {
        id: 15,
        nom: "azerty",
        type: "azerty",
        quantity: 456,
        prix: 25296
    },
    {
        id: 21,
        nom: "sdqs",
        type: "qsdqsd",
        quantity: 102,
        prix: 52
    },
    {
        id: 22,
        nom: "sdqs",
        type: "qsdqsd",
        quantity: 99,
        prix: 52
    }
];

var newDocs = _.map(docs, function(x) {return {type: x.type, quantity: x.quantity};});
var result = _.groupBy(newDocs, 'type');

console.log(result);

And the output looks like this:

{"azerty":[{"type":"azerty","quantity":456}],"qsdqsd":[{"type":"qsdqsd","quantity":102},{"type":"qsdqsd","quantity":99}]} 

Not sure what kind of chart library do you use and what input it expects but with lodash you will be for sure able to shape your data in any possible form.

UPDATE:

BTW, if you change map function a bit you can get type->quantity mapping as array (don't copy my code style :) ):

var newDocs = _.map(docs, function(x) {var tmp = {}; tmp[x.type] = x.quantity; return tmp;});

Output is the following:

[{"azerty":456},{"qsdqsd":102},{"qsdqsd":99}] 

6 Comments

i thought maybe if i can retreive the quanity data in a an array then type data in array maybe i could use it in nvd3 chart
it all depends on library and type of chart. I haven't used nvd3 but if you look in examples, you will see that each chart expects different input.
do you know a library less complicated in charts i want to view in every type how much quantity ps i know that's too much to ask .. so feel free to do what you want !! anyway Thank you for everything
nvd3 does not look complicated. you can use pie chart (nvd3.org/examples/pie.html). all you need to do is just to transform your data into required format (i.e., {label: x.type, value: x.quantity}) using lodash map function (see my answer)
i think your solution is the one i should accept it i find out that i was in a deadend until i came back here to tell you please how do we use lodash with angular :'(
|
1

Hello first of all what you have is already an array of objects , so I think that you dont need to create two other arrays and finally have 3 arrays.

Let's say that we keep your object on your question, and name it myData, we have:

If you want to log type & quantity values

myData.forEach(function(item) {
    console.log(item.type+'  +item.quantity);
})

or show the values Into your html template:

<table>
<tr ng-repeat="item in myData">
  <td>
     {{item.type}}
  </td>
  <td>{{item.quantity}}</td>
</tr>

Hope helps,good luck.

2 Comments

yeah i know the problem am trying to using in a chart so like am using a loop inside the data of the chart
Ok, but you already have your array , i still dont see the urgent to split it to two others. But its ok , since you found solution to your problem.
0

Try this

var x = [
    {
        id: 15,
        nom: "azerty",
        type: "azerty",
        quantity: 456,
        prix: 25296
    },
    {
        id: 21,
        nom: "sdqs",
        type: "qsdqsd",
        quantity: 102,
        prix: 52
    }
];

var type = [];
var quantity = [];

x.forEach(function(item, index) {
    type.push(item.type)
    quantity.push(item.quantity)
    if(index == x.length-1){
        console.log("type: ",type);    //type: ['azerty','qsdqsd']
        console.log("quantity: ",quantity);    // quantity : [456,102]
    }
})

// once forEach is done you'll have type=[azerty,qsdqsd]  and quantity = [456,102]

1 Comment

just glad i could help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.