简体   繁体   English

使用调用Angular / JavaScript * array.push(function(parameter))*中的函数的array.push时,修改数组中推送的对象

[英]Modify the object that is pushed in an array when using an array.push that calls a function in Angular/JavaScript *array.push(function(parameter))*

How can I append the object that is pushed into the array, when using a function in the push method of the array (I hope I am wording it correctly)? 当在数组的push方法中使用函数时,我怎样才能追加推入数组的对象(我希望我正确写入它)?

I have the following method that finds a match between a profile.id and a keywordId, and then pushes the matching kewordId into an array. 我有以下方法找到profile.id和keywordId之间的匹配,然后将匹配的kewordId推送到数组中。 How can I append this push method to return a modified object and not only the keywordId? 如何附加此push方法以返回修改后的对象而不仅仅是keywordId?

I would like to have the following pushed into the array: 我想将以下内容推入数组:

array.push({ id: profile.id, keyID: keywordID, name: profile.name });

But I am not sure how to append this line to enable this: 但我不知道如何附加此行来启用此功能:

array.push(findKeywordProfile(profile.id));

This is my method that creates the array: 这是我创建数组的方法:

function getKeywordProfiles(brandProfilesArray) {
  var array = [];
  brandProfilesArray.forEach(function (profile) {
      array.push(findKeywordProfile(profile.id));
  })
  return $q.all(array);
}

This is the method called during the array.push function: 这是在array.push函数中调用的方法:

function findKeywordProfile(brandProfileID) {
  var keywordProfileID = $q.defer();
  pullSocialMediaData('list_keyword_profiles.json').then(function (data) {
      var keywordProfileInstance = data.filter(function (keyword) {
          return keyword.brand_profile_id === brandProfileID;
      });
      keywordProfileID.resolve(keywordProfileInstance[0].id);
  });
  return keywordProfileID.promise;
}

Thank you! 谢谢!

You should pass the entire object in your function keywordProfileID 您应该在函数keywordProfileID传递整个对象

Something like, 就像是,

``` ```

function findKeywordProfile(brandProfileID) {
  var keywordProfileID = $q.defer();
  pullSocialMediaData('list_keyword_profiles.json').then(function (data) {
      var keywordProfileInstance = data.filter(function (keyword) {
          return keyword.brand_profile_id === brandProfileID;
      });
      keywordProfileID.resolve({id: keywordProfileInstance[0].id, name: keywordProfileInstance[0].id, keyID: keyword});
  });
  return keywordProfileID.promise;
}

``` ```

You can create an object from properties of keywordProfileInstance[0] , pass the object to resolve() 您可以从keywordProfileInstance[0]属性创建一个对象,将该对象传递给resolve()

let {id, keywordID, name} = keywordProfileInstance[0];
keywordProfileID.resolve({id, keyID:keywordID, name});

you should try this. 你应该试试这个。 hope it will work 希望它会奏效

function getKeywordProfiles(brandProfilesArray) {
  var array = [];
  brandProfilesArray.forEach(function (profile) {
      var data = findKeywordProfile(profile.id); 
      array.push(data);
  })
  return $q.all(array);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM