1

I have this object array:

var result = [
{
    appAddress:"127.0.0.1",
    name:"AppServer1",
    dbConnection:""
},
{
    appAdress:"",
    name:"DBServer1",
    dbConnection:"Server=.;Database=master;Integrated Security=SSPI"
}];

Now i need to get only the name values (into an array), where the appAddress isn't empty. I tried array.filter() and $.map() but none of these methods seems to do what i want.

This is what i tried:

var appServers = $.map(result, function (val, key) {
    return (key == 'appAddress' && val.length > 0) ? key : null;
    });

and

var appServers = result.filter(function (entry) {
    return entry['displayName'];
    });
4
  • Just an array of strings. Commented Jul 22, 2015 at 14:25
  • 3
    Show what you have tried. And this is quite a simple task to do yourself. Just loop the array, check the appAddress, if it's NOT empty, push it on to a new array that contains the results. Commented Jul 22, 2015 at 14:25
  • 2
    What did you try? What happened? .filter() and .map() are exactly what you need. Commented Jul 22, 2015 at 14:25
  • Thanks for showing what you've tried. Your problem with your use of $map is that you expect it to iterate over the properties of each object, but it does not: it iterates over the array. key will never be "appAddress", because $.map is operating on an array (which only has key names like 0, 1, 2, etc..), not the objects inside that array. As shown in Matthias's answer, you should use the first argument (val) to access each object and val.appAddress/val.name to get its property values. Commented Jul 22, 2015 at 14:34

5 Answers 5

2

First off, your object definitions are wrong. You need to use : instead of =.

Next, you can do this with a simple for loop:

var output = [];
for (var i = 0; i < result.length; i++) {
    var item = result[i];
    if (item.appAddress) output.push(item.name);
}

Here is a working example

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

Comments

1

For filtering you can use filter and map methods of Array. See the below example.

Array.prototype.filter Docs

Array.prototype.map Docs

var result = [{
  appAdress: "127.0.0.1",
  name: "AppServer1",
  dbConnection: ""
}, {
  appAdress: "",
  name: "DBServer1",
  dbConnection: "Server=.;Database=master;Integrated Security=SSPI"
}];


// First Solution
var out = result.filter(function(obj) {
  return obj.appAdress;
}).map(function(obj) {
  return obj.name;
})

// Second Solution
var namesArr = [];
result.forEach(function(obj) {
  if (obj.appAdress) {
    namesArr.push(obj.name);
  }
});



document.querySelector('#out').innerHTML = JSON.stringify(out, undefined, 4);
document.querySelector('#out2').innerHTML = JSON.stringify(namesArr, undefined, 4);
First Solution
<pre id="out"></pre>

<hr/>

Second Solution
<pre id="out2"></pre>

2 Comments

@musefan see the updated implementation. i have added map after filter.
This (first Solution) worked for me. First i need to filter and then map exposes the single object value. Thanks!
1

One thing you could probably do is

var names = result.filter(
    function (t) {
        return t.appAddress != "";
}).map(
    function (t) {
        return t.name;
});

2 Comments

Missed that part of the question X.X updated my answer.
Better to format your answer as code (4 spaces), rather that quote ( > )
1

You can use reduce like this:

var result = [
    {
        appAdress: "127.0.0.1",
        name: "AppServer1",
        dbConnection: ""
    },
    {
        appAdress: "",
        name: "DBServer1",
        dbConnection: "Server=.;Database=master;Integrated Security=SSPI"
    }];

var addresses = result.reduce(function (acc, it) {
    if (it.appAdress) {
        acc.push(it.name);
    }
    return acc;
}, []);

console.log(addresses);
Please check the result in console.

1 Comment

I keep forgetting about reduce +1
0

The objects defined in your result array are not valid. Object values are written as name:value pairs (separated by a colon).

Try this:

var result = [{
  appAdress: "127.0.0.1",
  name: "AppServer1",
  dbConnection: ""
}, {
  appAdress: "",
  name: "DBServer1",
  dbConnection: "Server=.;Database=master;Integrated Security=SSPI"
}];

var resultArr = $.map(result, function(val, i) {
  if (val.appAdress.length > 0) return val.name;
});

console.log(resultArr);
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Run on JSFiddle or read more about jQuery.map().

Comments

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.