问:
How to check a set of object is exist in another array object by jQuery
My array, stored in variable named data:

```<code>var data = [
  {
     "name": "username",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  },
  {
     "name": "password",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  },
  {
     "name": "gender",
     "value": "other",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.select",
        "choices": [
           {
              "value": "male",
              "label": "Male"
           },
           {
              "value": "female",
              "label": "Female"
           },
           {
              "value": "other",
              "label": "Other"
           }
        ]
     }
  },
  {
     "name": "country",
     "value": "3",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.select",
        "choices": [
           {
              "value": "1",
              "label": "Australia"
           },
           {
              "value": "2",
              "label": "USA"
           },
           {
              "value": "3",
              "label": "UK"
           }
        ]
     }
  },
  {
     "name": "decision",
     "value": "not sure",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.text",
        "choices": [
           {
              "value": "yes",
              "label": "Yes"
           },
           {
              "value": "no",
              "label": "No"
           },
           {
              "value": "not sure",
              "label": "I am not sure"
           }
        ]
     }
  },
  {
     "name": "hobby",
     "value": "['book', 'movie']",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.text",
        "choices": [
           {
              "value": "travel",
              "label": "Traveling"
           },
           {
              "value": "book",
              "label": "Reading"
           },
           {
              "value": "movie",
              "label": "Watching Movie"
           }
        ]
     }
  },
  {
     "name": "privacy",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  },
  {
     "name": "message",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  }
]
</code>```

My object, stored in variable named formObj:

```<code>var formObj = {

username: {
    value: null,
    type: 'text',
    label: 'Username',
    placeholder: 'Enter username',
    position: 'left',
    block: 'firstRow',
    order: 1,
    additionalClass: 'user'
  },
  password: {
    value: null,
    type: 'password',
    label: 'Password',
    placeholder: 'enter password',
    position: 'left',
    block: 'firstRow',
    order: 2
  },
  country: {
    value: '3',
    type: 'select',
    label: 'Where are you form?',
    //defaultText: 'Choose here',
    position: 'right',
    block: 'firstRow',
    order: 2,
    option: [{
        value: '1',
        label: 'Australia'
      },
      {
        value: '2',
        label: 'USA'
      },
      {
        value: '3',
        label: 'UK'
      }
    ]
  },
  gender: {
    value: 'female',
    type: 'select',
    label: 'Gender',
    defaultText: null,
    position: 'right',
    block: 'firstRow',
    order: 1,
    option: [{
        value: 'male',
        label: 'Male'
      },
      {
        value: 'female',
        label: 'Female'
      },
      {
        value: 'other',
        label: 'Other',
        additionalAttr: 'disabled'
      }
    ]
  },
  decision: {
    value: 'not sure',
    type: 'radio',
    label: 'Have you enjoyed it?',
    position: 'left',
    block: 'secondRow',
    order: 1,
    option: [{
        value: 'yes',
        label: 'Yes',
        additionalAttr: 'disabled'
      },
      {
        value: 'no',
        label: 'No',
      },
      {
        value: 'not sure',
        label: 'I am not sure',
      }
    ]
  },
  message: {
    value: null,
    type: 'textarea',
    label: 'Message',
    placeholder: 'type your message',
    position: 'right',
    block: 'secondRow',
    order: 2,
  },
  vehicle: {
    value: 'car',
    type: 'checkbox',
    label: 'Preferred vehicles',
    position: 'left',
    block: 'secondRow',
    order: 2,
    option: [{
        value: 'car',
        label: 'I like car'
      },
      {
        value: 'bike',
        label: 'I prefer bike'
      },
      {
        value: 'boat',
        label: 'Boat is my favorit'
      }
    ]
  },
  hobby: {
    value: ['book', 'movie'],
    type: 'checkbox',
    label: 'Hobbies',
    position: 'left',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'travel',
        label: 'Traveling'
      },
      {
        value: 'book',
        label: 'Reading'
      },
      {
        value: 'movie',
        label: 'Watching movie'
      }
    ]
  },
  privacy: {
    value: null,
    type: 'checkbox',
    label: 'Privacy',
    position: 'right',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'agree',
        label: 'I agree'
      }
    ]
  },
  email: {
    value: null,
    type: 'text',
    label: 'Email',
    placeholder: 'Enter email',
    position: 'right',
    block: 'secondRow',
    order: 1,
    additionalAttr: 'disabled',
    additionalClass: 'emailbox custom'
  }
}
</code>```

So, there are 8 objects in the data(array) and 10 objects in the formObj(object).

My target is matching the name from data with formObj, and create new object with the matched object only. All the value of the new object should come from the data(array). In short, create a newObj like/from formObj. newObj will not contain the object which are not in the data(array).

I have tried to write the concept here just, but don't understand how to go ahead:

```<code>for (var i = 0; i < data.length; i++) {
   if (data[i].name === formObj[key]) {
      newObj = formObj;   // take only those from formObj which exist in data
      newObj[option] = data[schema][choices];  // take the option from choices
      newObj[value] = data[value];  // take the value from value
   }
}
</code>```

So, the newObj will be:

```<code>var newObj = {

username: {
    value: null,
    type: 'text',
    label: 'Username',
    placeholder: 'Enter username',
    position: 'left',
    block: 'firstRow',
    order: 1,
    additionalClass: 'user'
  },
  password: {
    value: null,
    type: 'password',
    label: 'Password',
    placeholder: 'enter password',
    position: 'left',
    block: 'firstRow',
    order: 2
  },
  country: {
    value: '3',
    type: 'select',
    label: 'Where are you form?',
    //defaultText: 'Choose here',
    position: 'right',
    block: 'firstRow',
    order: 2,
    option: [{
        value: '1',
        label: 'Australia'
      },
      {
        value: '2',
        label: 'USA'
      },
      {
        value: '3',
        label: 'UK'
      }
    ]
  },
  gender: {
    value: 'other',
    type: 'select',
    label: 'Gender',
    defaultText: null,
    position: 'right',
    block: 'firstRow',
    order: 1,
    option: [{
        value: 'male',
        label: 'Male'
      },
      {
        value: 'female',
        label: 'Female'
      },
      {
        value: 'other',
        label: 'Other',
        additionalAttr: 'disabled'
      }
    ]
  },
  decision: {
    value: 'not sure',
    type: 'radio',
    label: 'Have you enjoyed it?',
    position: 'left',
    block: 'secondRow',
    order: 1,
    option: [{
        value: 'yes',
        label: 'Yes',
        additionalAttr: 'disabled'
      },
      {
        value: 'no',
        label: 'No',
      },
      {
        value: 'not sure',
        label: 'I am not sure',
      }
    ]
  },
  message: {
    value: null,
    type: 'textarea',
    label: 'Message',
    placeholder: 'type your message',
    position: 'right',
    block: 'secondRow',
    order: 2,
  },
  hobby: {
    value: ['book', 'movie'],
    type: 'checkbox',
    label: 'Hobbies',
    position: 'left',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'travel',
        label: 'Traveling'
      },
      {
        value: 'book',
        label: 'Reading'
      },
      {
        value: 'movie',
        label: 'Watching movie'
      }
    ]
  },
  privacy: {
    value: null,
    type: 'checkbox',
    label: 'Privacy',
    position: 'right',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'agree',
        label: 'I agree'
      }
    ]
  }
}
</code>```

My working code:

$.ajax({
  url: "https://api.myjson.com/bins/151f9r",
  method: "GET"
})
  .done(function(response) {
    var data = response.fieldset;
    console.log('.......................');
    console.log(data);
  })
  .fail(function(response) {
   console.log(response);
 });

var formObj = {
  username: {
    value: null,
    type: 'text',
    label: 'Username',
    placeholder: 'Enter username',
    position: 'left',
    block: 'firstRow',
    order: 1,
    additionalClass: 'user'
  },
  password: {
    value: null,
    type: 'password',
    label: 'Password',
    placeholder: 'enter password',
    position: 'left',
    block: 'firstRow',
    order: 2
  },
  country: {
    value: '3',
    type: 'select',
    label: 'Where are you form?',
    //defaultText: 'Choose here',
    position: 'right',
    block: 'firstRow',
    order: 2,
    option: [{
        value: '1',
        label: 'Australia'
      },
      {
        value: '2',
        label: 'USA'
      },
      {
        value: '3',
        label: 'UK'
      }
    ]
  },
  gender: {
    value: 'female',
    type: 'select',
    label: 'Gender',
    defaultText: null,
    position: 'right',
    block: 'firstRow',
    order: 1,
    option: [{
        value: 'male',
        label: 'Male'
      },
      {
        value: 'female',
        label: 'Female'
      },
      {
        value: 'other',
        label: 'Other',
        additionalAttr: 'disabled'
      }
    ]
  },
  decision: {
    value: 'not sure',
    type: 'radio',
    label: 'Have you enjoyed it?',
    position: 'left',
    block: 'secondRow',
    order: 1,
    option: [{
        value: 'yes',
        label: 'Yes',
        additionalAttr: 'disabled'
      },
      {
        value: 'no',
        label: 'No',
      },
      {
        value: 'not sure',
        label: 'I am not sure',
      }
    ]
  },
  message: {
    value: null,
    type: 'textarea',
    label: 'Message',
    placeholder: 'type your message',
    position: 'right',
    block: 'secondRow',
    order: 2,
  },
  vehicle: {
    value: 'car',
    type: 'checkbox',
    label: 'Preferred vehicles',
    position: 'left',
    block: 'secondRow',
    order: 2,
    option: [{
        value: 'car',
        label: 'I like car'
      },
      {
        value: 'bike',
        label: 'I prefer bike'
      },
      {
        value: 'boat',
        label: 'Boat is my favorit'
      }
    ]
  },
  hobby: {
    value: ['book', 'movie'],
    type: 'checkbox',
    label: 'Hobbies',
    position: 'left',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'travel',
        label: 'Traveling'
      },
      {
        value: 'book',
        label: 'Reading'
      },
      {
        value: 'movie',
        label: 'Watching movie'
      }
    ]
  },
  privacy: {
    value: null,
    type: 'checkbox',
    label: 'Privacy',
    position: 'right',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'agree',
        label: 'I agree'
      }
    ]
  },
  email: {
    value: null,
    type: 'text',
    label: 'Email',
    placeholder: 'Enter email',
    position: 'right',
    block: 'secondRow',
    order: 1,
    additionalAttr: 'disabled',
    additionalClass: 'emailbox custom'
  }
}

console.log(formObj);```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script></code>```

答:
You can use reduce to loop thru the data array.Check if the name exist as the key on formObj using hasOwnProperty. If it exist use spread syntax to shallow copy the object and overide the value property.

var data = [{"name":"username","value":null},{"name":"password","value":null},{"name":"gender","value":"other"},{"name":"country","value":"3"},{"name":"decision","value":"not sure"},{"name":"hobby","value":[]},{"name":"privacy","value":null},{"name":"message","value":null}]
var formObj = {"username":{"value":null,"type":"text","label":"Username","placeholder":"Enter username","position":"left"},"password":{"value":null,"type":"password","label":"Password","placeholder":"enter password","position":"left"},"gender":{"value":"female","type":"select","label":"Gender","defaultText":null,"position":"right"},"country":{"value":"3","type":"select","label":"Where are you form?","position":"right","block":"firstRow"},"decision":{"value":"not sure","type":"radio","label":"Have you enjoyed it?","position":"left","block":"secondRow"},"email":{"value":null,"type":"text","label":"Email","placeholder":"Enter email","position":"right"},"hobby":{"value":[null,null],"type":"checkbox","label":"Hobbies","position":"left","block":"secondRow"},"privacy":{"value":"","type":"checkbox","label":"Privacy","position":"right","block":"secondRow"},"message":{"value":"","type":"textarea","label":"Message","placeholder":"type your message","position":"right"},"vehicle":{"value":"car","type":"checkbox","label":"Preferred vehicles","position":"left","block":"secondRow"}}

var newObj = data.reduce((c, v) => {
  if (formObj.hasOwnProperty(v.name)) c[v.name] = {...formObj[v.name],value: v.value};
  return c;
}, {});

console.log(newObj);```

Body:
My array, stored in variable named data:

var data = [
  {
     "name": "username",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  },
  {
     "name": "password",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  },
  {
     "name": "gender",
     "value": "other",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.select",
        "choices": [
           {
              "value": "male",
              "label": "Male"
           },
           {
              "value": "female",
              "label": "Female"
           },
           {
              "value": "other",
              "label": "Other"
           }
        ]
     }
  },
  {
     "name": "country",
     "value": "3",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.select",
        "choices": [
           {
              "value": "1",
              "label": "Australia"
           },
           {
              "value": "2",
              "label": "USA"
           },
           {
              "value": "3",
              "label": "UK"
           }
        ]
     }
  },
  {
     "name": "decision",
     "value": "not sure",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.text",
        "choices": [
           {
              "value": "yes",
              "label": "Yes"
           },
           {
              "value": "no",
              "label": "No"
           },
           {
              "value": "not sure",
              "label": "I am not sure"
           }
        ]
     }
  },
  {
     "name": "hobby",
     "value": "['book', 'movie']",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.text",
        "choices": [
           {
              "value": "travel",
              "label": "Traveling"
           },
           {
              "value": "book",
              "label": "Reading"
           },
           {
              "value": "movie",
              "label": "Watching Movie"
           }
        ]
     }
  },
  {
     "name": "privacy",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  },
  {
     "name": "message",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  }
]

My object, stored in variable named formObj:

var formObj = {

username: {
    value: null,
    type: 'text',
    label: 'Username',
    placeholder: 'Enter username',
    position: 'left',
    block: 'firstRow',
    order: 1,
    additionalClass: 'user'
  },
  password: {
    value: null,
    type: 'password',
    label: 'Password',
    placeholder: 'enter password',
    position: 'left',
    block: 'firstRow',
    order: 2
  },
  country: {
    value: '3',
    type: 'select',
    label: 'Where are you form?',
    //defaultText: 'Choose here',
    position: 'right',
    block: 'firstRow',
    order: 2,
    option: [{
        value: '1',
        label: 'Australia'
      },
      {
        value: '2',
        label: 'USA'
      },
      {
        value: '3',
        label: 'UK'
      }
    ]
  },
  gender: {
    value: 'female',
    type: 'select',
    label: 'Gender',
    defaultText: null,
    position: 'right',
    block: 'firstRow',
    order: 1,
    option: [{
        value: 'male',
        label: 'Male'
      },
      {
        value: 'female',
        label: 'Female'
      },
      {
        value: 'other',
        label: 'Other',
        additionalAttr: 'disabled'
      }
    ]
  },
  decision: {
    value: 'not sure',
    type: 'radio',
    label: 'Have you enjoyed it?',
    position: 'left',
    block: 'secondRow',
    order: 1,
    option: [{
        value: 'yes',
        label: 'Yes',
        additionalAttr: 'disabled'
      },
      {
        value: 'no',
        label: 'No',
      },
      {
        value: 'not sure',
        label: 'I am not sure',
      }
    ]
  },
  message: {
    value: null,
    type: 'textarea',
    label: 'Message',
    placeholder: 'type your message',
    position: 'right',
    block: 'secondRow',
    order: 2,
  },
  vehicle: {
    value: 'car',
    type: 'checkbox',
    label: 'Preferred vehicles',
    position: 'left',
    block: 'secondRow',
    order: 2,
    option: [{
        value: 'car',
        label: 'I like car'
      },
      {
        value: 'bike',
        label: 'I prefer bike'
      },
      {
        value: 'boat',
        label: 'Boat is my favorit'
      }
    ]
  },
  hobby: {
    value: ['book', 'movie'],
    type: 'checkbox',
    label: 'Hobbies',
    position: 'left',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'travel',
        label: 'Traveling'
      },
      {
        value: 'book',
        label: 'Reading'
      },
      {
        value: 'movie',
        label: 'Watching movie'
      }
    ]
  },
  privacy: {
    value: null,
    type: 'checkbox',
    label: 'Privacy',
    position: 'right',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'agree',
        label: 'I agree'
      }
    ]
  },
  email: {
    value: null,
    type: 'text',
    label: 'Email',
    placeholder: 'Enter email',
    position: 'right',
    block: 'secondRow',
    order: 1,
    additionalAttr: 'disabled',
    additionalClass: 'emailbox custom'
  }
}

So, there are 8 objects in the data(array) and 10 objects in the formObj(object).

My target is matching the name from data with formObj, and create new object with the matched object only. All the value of the new object should come from the data(array). In short, create a newObj like/from formObj. newObj will not contain the object which are not in the data(array).

I have tried to write the concept here just, but don't understand how to go ahead:

for (var i = 0; i < data.length; i++) {
   if (data[i].name === formObj[key]) {
      newObj = formObj;   // take only those from formObj which exist in data
      newObj[option] = data[schema][choices];  // take the option from choices
      newObj[value] = data[value];  // take the value from value
   }
}

So, the newObj will be:

var newObj = {

username: {
    value: null,
    type: 'text',
    label: 'Username',
    placeholder: 'Enter username',
    position: 'left',
    block: 'firstRow',
    order: 1,
    additionalClass: 'user'
  },
  password: {
    value: null,
    type: 'password',
    label: 'Password',
    placeholder: 'enter password',
    position: 'left',
    block: 'firstRow',
    order: 2
  },
  country: {
    value: '3',
    type: 'select',
    label: 'Where are you form?',
    //defaultText: 'Choose here',
    position: 'right',
    block: 'firstRow',
    order: 2,
    option: [{
        value: '1',
        label: 'Australia'
      },
      {
        value: '2',
        label: 'USA'
      },
      {
        value: '3',
        label: 'UK'
      }
    ]
  },
  gender: {
    value: 'other',
    type: 'select',
    label: 'Gender',
    defaultText: null,
    position: 'right',
    block: 'firstRow',
    order: 1,
    option: [{
        value: 'male',
        label: 'Male'
      },
      {
        value: 'female',
        label: 'Female'
      },
      {
        value: 'other',
        label: 'Other',
        additionalAttr: 'disabled'
      }
    ]
  },
  decision: {
    value: 'not sure',
    type: 'radio',
    label: 'Have you enjoyed it?',
    position: 'left',
    block: 'secondRow',
    order: 1,
    option: [{
        value: 'yes',
        label: 'Yes',
        additionalAttr: 'disabled'
      },
      {
        value: 'no',
        label: 'No',
      },
      {
        value: 'not sure',
        label: 'I am not sure',
      }
    ]
  },
  message: {
    value: null,
    type: 'textarea',
    label: 'Message',
    placeholder: 'type your message',
    position: 'right',
    block: 'secondRow',
    order: 2,
  },
  hobby: {
    value: ['book', 'movie'],
    type: 'checkbox',
    label: 'Hobbies',
    position: 'left',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'travel',
        label: 'Traveling'
      },
      {
        value: 'book',
        label: 'Reading'
      },
      {
        value: 'movie',
        label: 'Watching movie'
      }
    ]
  },
  privacy: {
    value: null,
    type: 'checkbox',
    label: 'Privacy',
    position: 'right',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'agree',
        label: 'I agree'
      }
    ]
  }
}

My working code:

$.ajax({
  url: "https://api.myjson.com/bins/151f9r",
  method: "GET"
})
  .done(function(response) {
    var data = response.fieldset;
    console.log('.......................');
    console.log(data);
  })
  .fail(function(response) {
   console.log(response);
 });

var formObj = {
  username: {
    value: null,
    type: 'text',
    label: 'Username',
    placeholder: 'Enter username',
    position: 'left',
    block: 'firstRow',
    order: 1,
    additionalClass: 'user'
  },
  password: {
    value: null,
    type: 'password',
    label: 'Password',
    placeholder: 'enter password',
    position: 'left',
    block: 'firstRow',
    order: 2
  },
  country: {
    value: '3',
    type: 'select',
    label: 'Where are you form?',
    //defaultText: 'Choose here',
    position: 'right',
    block: 'firstRow',
    order: 2,
    option: [{
        value: '1',
        label: 'Australia'
      },
      {
        value: '2',
        label: 'USA'
      },
      {
        value: '3',
        label: 'UK'
      }
    ]
  },
  gender: {
    value: 'female',
    type: 'select',
    label: 'Gender',
    defaultText: null,
    position: 'right',
    block: 'firstRow',
    order: 1,
    option: [{
        value: 'male',
        label: 'Male'
      },
      {
        value: 'female',
        label: 'Female'
      },
      {
        value: 'other',
        label: 'Other',
        additionalAttr: 'disabled'
      }
    ]
  },
  decision: {
    value: 'not sure',
    type: 'radio',
    label: 'Have you enjoyed it?',
    position: 'left',
    block: 'secondRow',
    order: 1,
    option: [{
        value: 'yes',
        label: 'Yes',
        additionalAttr: 'disabled'
      },
      {
        value: 'no',
        label: 'No',
      },
      {
        value: 'not sure',
        label: 'I am not sure',
      }
    ]
  },
  message: {
    value: null,
    type: 'textarea',
    label: 'Message',
    placeholder: 'type your message',
    position: 'right',
    block: 'secondRow',
    order: 2,
  },
  vehicle: {
    value: 'car',
    type: 'checkbox',
    label: 'Preferred vehicles',
    position: 'left',
    block: 'secondRow',
    order: 2,
    option: [{
        value: 'car',
        label: 'I like car'
      },
      {
        value: 'bike',
        label: 'I prefer bike'
      },
      {
        value: 'boat',
        label: 'Boat is my favorit'
      }
    ]
  },
  hobby: {
    value: ['book', 'movie'],
    type: 'checkbox',
    label: 'Hobbies',
    position: 'left',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'travel',
        label: 'Traveling'
      },
      {
        value: 'book',
        label: 'Reading'
      },
      {
        value: 'movie',
        label: 'Watching movie'
      }
    ]
  },
  privacy: {
    value: null,
    type: 'checkbox',
    label: 'Privacy',
    position: 'right',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'agree',
        label: 'I agree'
      }
    ]
  },
  email: {
    value: null,
    type: 'text',
    label: 'Email',
    placeholder: 'Enter email',
    position: 'right',
    block: 'secondRow',
    order: 1,
    additionalAttr: 'disabled',
    additionalClass: 'emailbox custom'
  }
}

console.log(formObj);


来源: stackexchange

问:
Data structure that always keeps n-best elements
I need a data structure that always holds the n largest items inserted so far (in no particular order).

So, if n is 3, we could have the following session where I insert a few numbers and the content of the container changes:

```<code>[]  // now insert 1
[1] // now insert 0
[1,0] // now insert 4
[1,0,4] // now insert 3
[1,4,3] // now insert 0
[1,4,3] // now insert 3
[4,3,3]
</code>```

You get the idea. What's the name of the data structure? What's the best way to implement this? Or is this in some library?

I am thinking to use a container that has a priority_queue for its elements (delegation), which uses the reverse comparison, so pop will remove the smallest element. So the insert function first checks if the new element to be inserted is greater than the smallest. If so, we throw that smallest out and push the new element.

(I have a C++ implementation in mind, but the question is language-agnostic nevertheless.)

答: A bounded priority queue, I think... Java has something like this in its standard library. EDIT: it's called LinkedBlockingQueue. I'm not sure if the C++ STL includes something similar.

ContentLicense: CC BY-SA 2.5

Body:
I need a data structure that always holds the n largest items inserted so far (in no particular order).

So, if n is 3, we could have the following session where I insert a few numbers and the content of the container changes:

[]  // now insert 1
[1] // now insert 0
[1,0] // now insert 4
[1,0,4] // now insert 3
[1,4,3] // now insert 0
[1,4,3] // now insert 3
[4,3,3]

You get the idea. What's the name of the data structure? What's the best way to implement this? Or is this in some library?

I am thinking to use a container that has a priority_queue for its elements (delegation), which uses the reverse comparison, so pop will remove the smallest element. So the insert function first checks if the new element to be inserted is greater than the smallest. If so, we throw that smallest out and push the new element.

(I have a C++ implementation in mind, but the question is language-agnostic nevertheless.)

OwnerDisplayName: David


问:
CodeIgniter/jQuery - Ajax call returns full html page instead of my echo
In my view I have an ajax call:

```<code>    $(".previous").click(function() {
        $.ajax({
            type: "POST",
            url: "planner/get_cal",
            data: {current_month: current_month},
            success: function(msg){
                alert(msg);
            }

        });
</code>```

the get_cal function in my Planner controller:

```<code>function get_cal()
{
    echo "dinosaurs";
}
</code>```

However, instead of returning "dinosaurs", it returns a full HTML page. I can't figure out why. Thoughts? Thanks a lot.

答:
When using the CodeIgniter structure of Controler/Method uri segments I've found it much easier to use ../../controller/method as my URL's in jquery ajax requests. I also recommend specifying a dataType so that the string is parsed and returned as an object.

Here is an example;

```<code>
$.ajax({
     type: "POST",
     dataType: "json",
     url: "../../controller/method",
     success: mySuccessFunction
});
</code>```

ContentLicense: CC BY-SA 3.0

Body:
In my view I have an ajax call:

    $(".previous").click(function() {
        $.ajax({
            type: "POST",
            url: "planner/get_cal",
            data: {current_month: current_month},
            success: function(msg){
                alert(msg);
            }

        });

the get_cal function in my Planner controller:

function get_cal()
{
    echo "dinosaurs";
}

However, instead of returning "dinosaurs", it returns a full HTML page. I can't figure out why. Thoughts? Thanks a lot.

Body:
When using the CodeIgniter structure of Controler/Method uri segments I've found it much easier to use ../../controller/method as my URL's in jquery ajax requests. I also recommend specifying a dataType so that the string is parsed and returned as an object.

Here is an example;

$.ajax({
     type: "POST",
     dataType: "json",
     url: "../../controller/method",
     success: mySuccessFunction
});